Results 1 to 7 of 7
Thread: Longs are read a Ints
- 10-02-2011, 03:13 AM #1
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.
- 10-02-2011, 03:15 AM #2
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Re: Longs are read a Ints
Change
toJava Code:for(double i = 2; i < num+1; i += 1)
EDIT: Read post #6.Java Code:for(long i = 2; i < num+1; i += 1)"
Last edited by Solarsonic; 10-02-2011 at 03:30 AM.
- 10-02-2011, 03:17 AM #3
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."
- 10-02-2011, 03:19 AM #4
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 10-02-2011, 03:21 AM #5
Re: Longs are read a Ints
Line 8, "long num = 600851475143;"
I'm using Eclipse, by the way
- 10-02-2011, 03:26 AM #6
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 10-02-2011, 03:31 AM #7
Similar Threads
-
Boolean operators on ints
By rockgardenlove in forum New To JavaReplies: 3Last Post: 02-10-2011, 04:14 AM -
Sum of odd ints 0-n
By falkon114 in forum New To JavaReplies: 7Last Post: 12-06-2010, 08:36 AM -
Help with ints
By Insomniac Riot in forum New To JavaReplies: 5Last Post: 04-02-2010, 03:53 PM -
checking for ints in a String
By SteroidalPsycho in forum New To JavaReplies: 1Last Post: 03-26-2010, 06:09 PM -
GUI's and inputting doubles or ints
By lopder1 in forum New To JavaReplies: 19Last Post: 11-05-2009, 08:50 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks