Factorial loop results are incorrect after the 5th iteration

前端 未结 4 2050
离开以前
离开以前 2021-01-19 07:38

I am currently taking pre-calculus and thought that I would make a quick program that would give me the results of factorial 10. While testing it I noticed that I was gettin

相关标签:
4条回答
  • 2021-01-19 07:53

    That is an Integer Overflow issue. 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 overflow, theoretically)

    The basic idea is that signed int stores numbers between -2,147,483,648 to 2,147,483,647, which are stored as binary bits (all information in a computer are stored as 1's and 0's)

    Positive numbers are stored with 0 in the most significant bit, and negative numbers are stored with 1 in the most significant bit. If your positive number gets too big in binary representation, digits will carry over to the signed bit and turn your positive number into the binary representation of a negative one.

    Then when the factorial gets bigger than even what an unsigned int can store, it will "wrap around" and lose the carry-over from its most significant (signed) bit - that's why you are seeing the pattern of sometimes alternating positive and negative values in your output.

    0 讨论(0)
  • 2021-01-19 08:02

    You're surpassing the capacity of the int type (2,147,483,647), so your result is wrapping back around to the minimum int value. Try using long instead.

    Having said the that, the method you are currently employing will not result in the correct answer: actually, you are currently computing 10! ^ 2.

    Why complicate things? You could 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 successive factorials until 10! is reached.

    Also, as others have mentioned, if you need values bigger than what long can support you should use BigInteger, which supports arbitrary precision.

    0 讨论(0)
  • 2021-01-19 08:02

    In addition to what the other answers mention about the overflow, your factorial algorithm is also incorrect. 10! should calculate 10*9*8*7*6*5*4*3*2*1, you are doing (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 the factorial of higher numbers, but int is plenty large enough to calculate the factorial of 10.

    0 讨论(0)
  • 2021-01-19 08:11

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

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

    But the wrong results are coming from the fact that you reached the maximum value for the type int as pointed out 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 );
    
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题