Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
905 views
in Technique[技术] by (71.8m points)

integer - The range of int in Java

I understand that the int range in Java should be -2^31 to 2^31-1.

But when I run this code snippet with 20:

public class Factorial {
    public int factorial(int n) {
        int fac=1;
        for (int i=1; i<=n; i++) {
            fac *= i;
            System.out.println("Factorial of " + i + " is: " + fac);
        }
        return fac;
    }
}

The output:

Factorial of 1 is: 1
Factorial of 2 is: 2
Factorial of 3 is: 6
Factorial of 4 is: 24
Factorial of 5 is: 120
Factorial of 6 is: 720
Factorial of 7 is: 5040
Factorial of 8 is: 40320
Factorial of 9 is: 362880
Factorial of 10 is: 3628800
Factorial of 11 is: 39916800
Factorial of 12 is: 479001600
Factorial of 13 is: 1932053504
Factorial of 14 is: 1278945280
Factorial of 15 is: 2004310016
Factorial of 16 is: 2004189184
Factorial of 17 is: -288522240
Factorial of 18 is: -898433024
Factorial of 19 is: 109641728
Factorial of 20 is: -2102132736

It's not making sense from 13 (13! = 6,227,020,800). It looks like it's out of range and wrapped around. What's wrong? Is it due to Eclipse that I'm using?

Though I think it's not relevant, here is the test code:

public class TestFac {

    public static void main(String[] args) {
        int n;
        Scanner sc = new Scanner(System.in);

        System.out.println("Input num you want to factorial: ");
        n = sc.nextInt();
        Factorial fac = new Factorial();
        fac.factorial(n);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here I would like to mention the concept of integer clock.

The maximum and minimum values for int in Java are:

int MAX_VALUE = 2147483647
int MIN_VALUE = -2147483648

Please check the following results

 int a = 2147483645;
 for(int i=0; i<10; i++) {
    System.out.println("a:" + a++);
 }

Output:

a:2147483645
a:2147483646
a:2147483647
a:-2147483648
a:-2147483647
a:-2147483646
a:-2147483645
a:-2147483644
a:-2147483643
a:-2147483642

It shows that when you go beyond the limit of the +ve range of integer, the next values starts from its negative starting value again.

 -2147483648,       <-----------------
 -2147483647,                        |
 -2147483646,                        |
  .                                  |
  .                                  |
  .                                  |    (the next value will go back in -ve range)
  0,                                 |
 +1,                                 |
 +2,                                 |
 +3,                                 |
  .                                  |
  .                                  |
  .,                                 |
 +2147483645,                        |
 +2147483646,                        |
 +2147483647     ---------------------

If you calculate the factorial of 13 it is 6227020800. This value goes beyond the int range of java. So the new value will be

        6227020800
      - 2147483647 (+ve max value)
   -----------------
Value = 4079537153
      - 2147483648 (-ve max value)
   -----------------
value = 1932053505
   -             1  (for zero in between -ve to +ve value)
  ----------------
Answer = 1932053504

So, in your answer, the factorial of 13 is becoming 1932053504. This is how integer clock works.

You can use long datatype instead of integer to achieve your purpose.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...