Factorial loop results incorrect after 5th iteration

I am currently doing preliminary calculus and think that I will make a quick program that will give me the results of factorial 10. During testing, I noticed that after the 5th iteration I got wrong results. However, the first 4 iterations are correct.

public class Factorial { public static void main(String[] args) { int x = 1; int factorial; for(int n = 10; n!=1; n--) { factorial = n*(n-1); x = x * factorial; System.out.printf("%d ", x); } }//end of class main }//end of class factorial 

Why am I getting negative values

+4
source share
4 answers

You outperform int type (2,147,483,647), so your result wraps up to a minimum int value. Use long instead.

Having said that, the method you are currently using will not lead to the correct answer: in fact, you are currently calculating 10! ^ 2 10! ^ 2 .

Why complicate things? You can easily do something like this:

 long x = 1L; for(int n = 1; n < 10; n++) { x *= n; System.out.println(x); } 
  1
 2
 6
 24
 120
 720
 5040
 40320
 362880

which shows consecutive factorials until reaching 10! .

Also, as others have said, if you need values ​​greater than long , you can use BigInteger , which supports arbitrary precision.

+7
source

This is an integer overflow . Use long or unsigned long instead of int . (And, as @Dunes suggested, your best bet is really BigInteger when working with very large numbers, because it will never theoretically overflow)

The basic idea is that signed -2,147,483,648 to 2,147,483,647 store numbers between -2,147,483,648 to 2,147,483,647 , which are stored as binary bits (all information on the computer is stored as 1 and 0 )

Positive numbers are stored from 0 in the most significant bit, and negative numbers are stored from 1 in the most significant bit. If your positive number becomes too large in binary representation, the numbers will be transferred to the signed bit and turn your positive number into the binary representation of the negative.

Then, when the factorial becomes larger than even the one that can store the unsigned int , it will “turn around” and lose the transfer from its most significant (signed) bit - this is why you see a pattern of sometimes alternating positive and negative values ​​in your output.

+9
source

Your formula for factorial is incorrect. What you will have is:

  • Step 1: n * (n-1) = 10 * 9 = 90 => x = 1 * 90 = 90
  • Step 2: n * (n-1) = 9 * 8 = 72 => x = 90 * 72 = 6480 or it should be: 10 * 9 * 8 => 720

But the wrong results come from the fact that you have reached the maximum value for the int type, as indicated by others

Your code should be

 public class Factorial { public static void main(String[] args) { double factorial = 1; for(int n = factorial; n>=1; n--) { factorial = factorial * n; System.out.printf("%d ", factorial ); } } } 
+3
source

In addition to the other answers mentioning overflow, your factor algorithm is also incorrect. ten! should calculate 10*9*8*7*6*5*4*3*2*1 , you do (10*9)*(9*8)*(8*7)*(7*6)*...

Try changing your loop to the following:

 int x = 1; for(int n = 10; n > 1 ; n--) { x = x * n; System.out.printf("%d ", x); } 

You will eventually overflow if you try to calculate factorial with higher numbers, but int is big enough to calculate factorial 10.

+2
source

All Articles