I understand that 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. Looks like it's out of range and wraped 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);
}
}
Here I would like to mention the concept of integer clock.
Max and min 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 +ve range of integer, the next values starts from its negative starting value again.
-2147483648, <-----------------
-2147483647, |
-2147483646, |
. |
. |
. | (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 int range of java.
So 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 factorial of 13 is coming 1932053504.
This is how integer clock works.
You can use long datatype instead of integer to achieve your purpose.
For any queries you can post here.
From http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html:
The int
data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.
Moral of the story: Never believe in your teacher blindly!
If you check Java Integer, its maximum and minimum values are as follows:
int MAX_VALUE = 2147483647
int MIN_VALUE = -2147483648
If you do some maths, you will see that (Factorial of 13) 1932053504 * 14
is 27048749056
which is beyond int MAX_VALUE and that's why you are getting wrong result for Factorial of 14. So in order to have good results it is better to use long
type instead.
Factorial 13 is 6227020800. This is more than 31 bits long so it's wrapped around.
If you want to support BIG numbers (e.g. arbitrary length) then consider using the BigInteger class which provides an unlimited range.
Please run this code:
System.out.println("Minimum value of Integer is: " + Integer.MIN_VALUE);
System.out.println("Maximum value of Integer is: " + Integer.MAX_VALUE);
So you can see why it fails.