Factorial For Loop only works up to 12

feelingstoned

Given my code:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Fact_2 {
  public static void main(String args[]) throws IOException {
    System.out.println("Please enter a number:");
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    int fact = Integer.parseInt(input.readLine());

    int factorial = 1;
    for (int i = 1; i <= fact; i++) {
        factorial = factorial * i;
    }
    System.out.println("The factorial of " + fact + " is " + factorial);
}
}

The program works correctly...only up to the 12th digit. I checked to make sure all the factorials were right but when you enter 13 for your number you get 1932053504 when it should be 6227020800. Why is that?

Ian

I just want to add the mathematical reasoning with regards to the integer overflow:

12! = 479,001,600
13! = 6,227,020,800

Now, the range limit for int (32-bit) type is:

-2,147,483,648 to 2,147,483,647

which is exceeded just when the factorial becomes 13 since:

479,001,600 < 2,147,483,647 < 6,227,020,800

Because of the overflow, when you have 13 factorial, it treats it as:

13! = 6,227,020,800 % 4,294,967,296 
    = 1,932,053,504 + 4,294,967,296 x 1 % 4,294,967,296
    = 1,932,053,504

To fix it, use BigInteger. If you don't need it to be too large, use long which has capacity of:

 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

long can handle up to 20!

20! = 2,432,902,008,176,640,000

Beyond that, you need to use BigInteger

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Factorial For Loop only works up to 12

From Dev

Doing a factorial with only one while loop

From Dev

How Factorial logic works?

From Dev

isspace() only works for digits up to 8?

From Dev

parseInt() only works seem to up to 9999

From Dev

Finding factorial of factorial up to K times

From Java

Loop to remove duplicates only works for the first iteration

From Dev

Adding addEventListener() in loop only works for last button

From Dev

loop animate javascript, SetInterval works only once

From Dev

Serial.println works only inside of loop()

From Dev

my loop only works for the last file R

From Dev

Adding addEventListener() in loop only works for last button

From Dev

Python:For loop only works once in python

From Dev

Nested IF inside FOR loop works only for first value

From Dev

Writing a For Loop to Evaluate a Factorial

From Dev

Ruby factorial loop

From Dev

Weird IE, Javascript only works in development mode (F12)

From Dev

Java merge sort on strings only works on arraylist up to four elements

From Dev

How to set up vpn so that it only works on custom urls in linux

From Dev

Toggling Up and Down hidden paragraphs; only one works

From Dev

Factorial(s) of N numbers in for loop

From Dev

Explanation of "int factorial = 1" in this for loop

From Dev

Write factorial with while loop python

From Dev

C# foreach loop works only with last item

From Dev

PHP While loop of ID's only works with first id

From Dev

do-while loop only works when condition uses "and" not "or"

From Dev

Jquery Show/Hide only works on first row of php loop

From Dev

PHP While loop only works for first two of three results?

From Dev

Reading a file with IFS loop works only when no arrays are used

Related Related

HotTag

Archive