108
public class Three {
    public static void main(String[] args) {
        Three obj = new Three();
        obj.function(600851475143);
    }

    private Long function(long  i) {
        Stack<Long> stack = new Stack<Long>();

        for (long j = 2; j <= i; j++) {
            if (i % j == 0) {
                stack.push(j);
            }
        }
        return stack.pop();
    }
}

When the code above is run, it produces an error on the line obj.function(600851475143);. Why?

tobias_k
  • 81,265
  • 12
  • 120
  • 179
user446654
  • 1,111
  • 2
  • 7
  • 7
  • 1
    also there is no difference between "l" and "L"? – user446654 Sep 21 '10 at 06:23
  • @user446654: No, there is. The latter is more readable. Read "Java Puzzler" for this. – Adeel Ansari Sep 21 '10 at 06:30
  • @user446654: evolving @Thilo thoughts about possible memory limit exceeding I want to add my 2 coins: you've chosen really, really bad algorithm for searching all dividers of a number if you want to operate with such big numbers as in your example. Something based on *dynamic programming* would work better probably. Google on that for further results. – Roman Sep 21 '10 at 06:31
  • 1
    Added PE tag, Project Euler #3 – st0le Sep 21 '10 at 06:55
  • @st0le: IMHO the question is deffinitely not about original problem solution, and what we see is not a solution as well. – Roman Sep 21 '10 at 07:12

8 Answers8

243

600851475143 cannot be represented as a 32-bit integer (type int). It can be represented as a 64-bit integer (type long). long literals in Java end with an "L": 600851475143L

Yuliy
  • 17,381
  • 6
  • 41
  • 47
90

Append suffix L: 23423429L.

By default, java interpret all numeral literals as 32-bit integer values. If you want to explicitely specify that this is something bigger then 32-bit integer you should use suffix L for long values.

Roman
  • 64,384
  • 92
  • 238
  • 332
  • For those looking for a more thorough explanation of why you get this error message even after you change the variable type to `long`, read this: https://stackoverflow.com/a/8924925/293280 – Joshua Pinter Oct 17 '19 at 02:47
37

You need to use a long literal:

obj.function(600851475143l);  // note the "l" at the end

But I would expect that function to run out of memory (or time) ...

Thilo
  • 257,207
  • 101
  • 511
  • 656
16

The java compiler tries to interpret 600851475143 as a constant value of type int by default. This causes an error since 600851475143 can not be represented with an int.

To tell the compiler that you want the number interpretet as a long you have to add either l or L after it. Your number should then look like this 600851475143L.

Since some Fonts make it hard to distinguish "1" and lower case "l" from each other you should always use the upper case "L".

josefx
  • 15,506
  • 6
  • 38
  • 63
7

You need 40 bits to represent the integer literal 600851475143. In Java, the maximum integer value is 2^31-1 however (i.e. integers are 32 bit, see http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html).

This has nothing to do with function. Try using a long integer literal instead (as suggested in the other answers).

Andre Holzner
  • 18,333
  • 6
  • 54
  • 63
5

At compile time the number "600851475143" is represented in 32-bit integer, try long literal instead at the end of your number to get over from this problem.

JVM
  • 229
  • 1
  • 2
4

Apart from all the other answers, what you can do is :

long l = Long.parseLong("600851475143");

for example :

obj.function(Long.parseLong("600851475143"));
Anand Undavia
  • 3,493
  • 5
  • 19
  • 33
3

Or, you can declare input number as long, and then let it do the code tango :D ...

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    System.out.println("Enter a number");
    long n = in.nextLong();

    for (long i = 2; i <= n; i++) {
        while (n % i == 0) {
            System.out.print(", " + i);
            n /= i;
        }
    }
}