Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Solarsonic

Thread: Longs are read a Ints

  1. #1
    nhmllr's Avatar
    nhmllr is offline Member
    Join Date
    Aug 2011
    Posts
    51
    Rep Power
    0

    Default Longs are read as Ints (still unsolved)

    So I'm working through project euler, and problem 4 calls for a big number. It's outside of int territory, but inside of long territory. So I declare it as a long, yet it errors because it's too big for an int

    "The literal 600851475143 of type int is out of range "

    If anyone's curious, here's the code

    Java Code:
    //Find the largest prime factor of a composite number (600851475143).
    
    public class problem3 {
    	
    	//main
    	public static void main(String[] args) 
        {
    		long num = 600851475143;
    
    		for(double i = 2; i < num+1; i += 1)
    		{
    			if(num%i == 0)
    			{
    				System.out.println(i);
    				num /= i;
    				i -= 1;
    			}
    		}
        }
    }
    Last edited by nhmllr; 10-02-2011 at 03:24 AM.

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: Longs are read a Ints

    Change

    Java Code:
    for(double i = 2; i < num+1; i += 1)
    to

    Java Code:
    for(long i = 2; i < num+1; i += 1)"
    EDIT: Read post #6.
    Last edited by Solarsonic; 10-02-2011 at 03:30 AM.

  3. #3
    nhmllr's Avatar
    nhmllr is offline Member
    Join Date
    Aug 2011
    Posts
    51
    Rep Power
    0

    Default Re: Longs are read a Ints

    Whoops I meant for i to be an int (but I forgot to change it back after some experimenting)

    In anycase, I changed it to "for(long i = 2; i < num+1; i += 1)" and it still errors the same way.

    It's erroring at the declaration of "num", not on "i."

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: Longs are read a Ints

    Quote Originally Posted by nhmllr View Post
    Whoops I meant for i to be an int (but I forgot to change it back after some experimenting)

    In anycase, I changed it to "for(long i = 2; i < num+1; i += 1)" and it still errors the same way.

    It's erroring at the declaration of "num", not on "i."
    What line is giving you the error?

  5. #5
    nhmllr's Avatar
    nhmllr is offline Member
    Join Date
    Aug 2011
    Posts
    51
    Rep Power
    0

    Default Re: Longs are read a Ints

    Line 8, "long num = 600851475143;"

    I'm using Eclipse, by the way

  6. #6
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: Longs are read a Ints

    Quote Originally Posted by nhmllr View Post
    Line 8, "long num = 600851475143;"

    I'm using Eclipse, by the way
    In order to use a long that's larger than the maximum value of an int, you need to make it a literal.

    Change

    Java Code:
    long num = 600851475143;
    to

    Java Code:
    long num = 600851475143L;
    Fubarable likes this.

  7. #7
    nhmllr's Avatar
    nhmllr is offline Member
    Join Date
    Aug 2011
    Posts
    51
    Rep Power
    0

    Default Re: Longs are read a Ints

    Huh. I've never seen that.
    In anycase, I put in that little "L" and I got the problem right!

    Thanks a lot!

Similar Threads

  1. Boolean operators on ints
    By rockgardenlove in forum New To Java
    Replies: 3
    Last Post: 02-10-2011, 04:14 AM
  2. Sum of odd ints 0-n
    By falkon114 in forum New To Java
    Replies: 7
    Last Post: 12-06-2010, 08:36 AM
  3. Help with ints
    By Insomniac Riot in forum New To Java
    Replies: 5
    Last Post: 04-02-2010, 03:53 PM
  4. checking for ints in a String
    By SteroidalPsycho in forum New To Java
    Replies: 1
    Last Post: 03-26-2010, 06:09 PM
  5. GUI's and inputting doubles or ints
    By lopder1 in forum New To Java
    Replies: 19
    Last Post: 11-05-2009, 08:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •