1

Problem: I can't store the number '600851475143'. I realize this number is bigger than what an int can hold and is smaller than the maximum long value. However, my program isn't registering the variable "number" as a long, it is registering it as a int. Can someone shed some light onto this problem?

** - Line of the problem

public class Problem3{
//What is the largest prime factor of the number 600851475143
public static void main(String[] args){
  ***long number = 600851475143 , total = 0;
    for(long x = (number-1)/2; x>1; x--)
      if(number%x == 0 && isPrime(x)) total += x;
    System.out.println(total);
}
private static boolean isPrime(long determine){
  for(long x = determine/2 - 1; x>1; x--)
    if(determine%x ==0) return false;
  return true;
}

}

SOLUTION: As Jim said below, in order to of type long, one has to put a "L" or "l" at the end of the number. "An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int. It is recommended that you use the upper case letter L because the lower case letter l is hard to distinguish from the digit 1." - From Oracle site on Primitive types.

A little more info: Java's L number (long) specification

2
  • 3
    I see 2147483647 in your code, not 600851475143. Not that that should cause number to be an int and not a long... Commented May 11, 2012 at 23:46
  • Sorry, I was trying to solve my issue. The "2147483647" is the max number that can be stored as an int. I realized that any number above this number was causing an error. I edited my original post to avoid potential confusion for people in the future. Commented May 12, 2012 at 3:52

3 Answers 3

12

Long literals need to be expressed with a trailing "L", as in 600851475143L

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Jim. This helped a lot.
4

Put a small 'L' in this literal value:

 600851475143L

The reason is:

An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int. It is recommended that you use the upper case letter L because the lower case letter l is hard to distinguish from the digit 1.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

You can use this notation too, to be more clear:

 600_851_475_143L

3 Comments

Doesn't have to be a small 'l', it can be a big L too.
I recommend against using lowercase L, since it often looks like a 1 (one). Instead, use uppercase L.
Yes, sometimes is hard to distinguish l from 1. Thanks
4

"my program isn't registering the variable "number" as a long, it is registering it as a int".

That's not correct. You declared it as a long. It's a long.

What you must be getting is something quite different: a compile error on the constant 600851475143. Try 600851475143L. I suggest that if you read the compiler error message more carefully you would have seen that.

2 Comments

Thank you, I did further research after. My compiler only said "Error: integer number too large: 6008514751143".
@user1390463 Exactly as I said. The compiler has told you exactly what is wrong. Your first mistake was to mangle what the message told you into "isn't registering the variable "number" as a long".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.