2016-10-09 3 views
-3

これは私の割り当ての一部だったと5と7階乗を計算すると、どこが間違っていましたか?

の階乗を計算するように頼まれた私は以下のようにそれを終え:

import java.util.Scanner; 
    public class Factorial { 
     public static void main(String [] args) 
     { 
      System.out.println("Please enter a number: "); 
      Scanner input=new Scanner(System.in); 
      int number=input.nextInt(); 
      int i,fact=1; 

       for(i=1;i<=number;i++){  
        fact=fact*i;  
       }  
       System.out.println("Factorial of " + number + " is: " + fact);   
     } 
    } 

それは(120と5040をもたらす)5と7のために働きました。 私の教授が来て、20と987654321でテストしたところ、結果は-2102132736と0を返します。

なぜですか?

P.S.私は987654321のケースでは、アプリケーションが壊れてしまうか、エラーが返ってくる可能性が高いと考えました。

+4

整数オーバーフロー(クラッシュなし)。 'fact'を' long'(または 'BigInteger')に変更してください。 –

+0

誰かが大きい数字でそれを使うのを知っているなら、おそらく "Int"の代わりに "Long"を使うべきでしょう。しかし、なぜ、987654321の階乗の結果が0を返すのはなぜですか? – Meruemu

+1

[Javaは10から99までのすべての数値の積が0だと思うのはなぜですか?](// stackoverflow.com/q/26375932) – Tom

答えて

1

5040!非常に(さらにはlongオーバーフロー)です。 BigIntegerのように使用

System.out.println("Please enter a number: "); 
Scanner input = new Scanner(System.in); 
int number = input.nextInt(); 
BigInteger fact = BigInteger.ONE; 
for (int i = 2; i <= number; i++) { // <-- x * 1 = x 
    fact = fact.multiply(BigInteger.valueOf(i)); 
} 
System.out.println("Factorial of " + number + " is: " + fact); 
+0

987654321の階乗の結果を得ることができますか? – saka1029

+0

@はい。それはちょうど非常に大きいです。 –

+1

このアルゴリズムは大きな数を処理する能力を持っていますが、速度に関しては非常に非効率的です。より良い[もっとスマートなアルゴリズム](http://www.luschny.de/math/factorial/FastFactorialFunctions.htm)を使うこと。 – 4castle

2

このコードは、あなたの問題を解決する可能性があります。 It is taken from here

class BigFactorial 
{ 
    static void factorial(int n) 
    { 
     int res[] = new int[300]; 

     // Initialize result 
     res[0] = 1; 
     int res_size = 1; 

     // Apply simple factorial formula n! = 1 * 2 * 3 * 4...*n 
     for (int x=2; x<=n; x++) 
      res_size = multiply(x, res, res_size); 

     System.out.println("Factorial of given number is: "); 
     for (int i=res_size-1; i>=0; i--) 
      System.out.print(res[i]); 
    } 

    // This function multiplies x with the number represented by res[]. 
    // res_size is size of res[] or number of digits in the number represented 
    // by res[]. This function uses simple school mathematics for multiplication. 
    // This function may value of res_size and returns the new value of res_size 
    static int multiply(int x, int res[], int res_size) 
    { 
     int carry = 0; // Initialize carry 

     // One by one multiply n with individual digits of res[] 
     for (int i=0; i<res_size; i++) 
     { 
      int prod = res[i] * x + carry; 
      res[i] = prod % 10; // Store last digit of 'prod' in res[] 
      carry = prod/10; // Put rest in carry 
     } 

     // Put carry in res and increase result size 
     while (carry!=0) 
     { 
      res[res_size] = carry%10; 
      carry = carry/10; 
      res_size++; 
     } 
     return res_size; 
    } 

    // Driver program 
    public static void main(String []args) 
    { 
     factorial(100); 

    } 

} 
1

これは、あなたが保存すると、あなたの結果を印刷するために取ったコンテナが、このような大きな整数を(私は20の階乗を意味する)を保持する能力を持っていないという事実です。だから、大きな容器が必要です。既に提案されているように、BIGINTEGERを使うことができます。

関連する問題