16

これを行うとき:

int x = 100;
int result = 1;
for (int i = 1; i < (x + 1); i++) {
    result = (result * i);
}
System.out.println(result);

これは明らかに、結果が整数に対して大きすぎるためですが、私はオーバーフローに対して0ではなく大きな負の数を取得するために使用されます。

前もって感謝します!


これに切り替えると:

int x = 100;
int result = 1;

for (int i = 1; i < (x + 1); i++) {
    result = (result * i);
    System.out.println(result);
}

私はこれを手に入れます

4

10 に答える 10

26

1から100までの50の偶数があります。これは、階乗が少なくとも50回2の倍数であることを意味します。つまり、2進数として、最後の50ビットは0になります(実際には、2番目の偶数が2 * 2の倍数であるなど)。

public static void main(String... args) {
    BigInteger fact = fact(100);
    System.out.println("fact(100) = " + fact);
    System.out.println("fact(100).longValue() = " + fact.longValue());
    System.out.println("fact(100).intValue() = " + fact.intValue());
    int powerOfTwoCount = 0;
    BigInteger two = BigInteger.valueOf(2);
    while (fact.compareTo(BigInteger.ZERO) > 0 && fact.mod(two).equals(BigInteger.ZERO)) {
        powerOfTwoCount++;
        fact = fact.divide(two);
    }
    System.out.println("fact(100) powers of two = " + powerOfTwoCount);
}

private static BigInteger fact(long n) {
    BigInteger result = BigInteger.ONE;
    for (long i = 2; i <= n; i++)
        result = result.multiply(BigInteger.valueOf(i));
    return result;
}

プリント

fact(100) = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
fact(100).longValue() = 0
fact(100).intValue() = 0
fact(100) powers of two = 97

これは、97ビット整数がfact(100)の最下位ビットに対して0になることを意味します。

実際、fact(n)の2の累乗の数はnに非常に近いです。fact(10000)の場合、9995の2の累乗があります。これは、それがおよそ1/2のn乗の合計であり、合計がに近いためnです。つまり、2つおきの数はn / 2であり、4つおきには2(+ n / 4)の追加の累乗があり、8つおきには(+ n / 8)の追加の累乗がありnます。

于 2011-03-15T20:51:45.740 に答える
21

大きな負の数は、特定の範囲にオーバーフローした値です。factorial(100)最後に32を超えるバイナリゼロがあるため、整数に変換するとゼロが生成されます。

于 2011-03-15T20:41:19.157 に答える
8

原因を調べるために、階乗の素因数分解を観察することができます。

fac( 1) = 1             = 2^0
fac( 2) = 2             = 2^1
fac( 3) = 2 * 3         = 2^1 * 3
fac( 4) = 2 * 2 * 2 * 3 = 2^3 * 3
fac( 5) =  ...          = 2^3 * 3 * 5
fac( 6) = ...           = 2^4 * 3^2 * 5
fac( 7) = ...           = 2^4 * ...
fac( 8) = ...           = 2^7 * ...
fac( 9) = ...           = 2^7 * ...
fac(10) = ...           = 2^8 * ...
fac(11) = ...           = 2^8 * ...
...
fac(29) = ...           = 2^25 * ...
fac(30) = ...           = 2^26 * ...
fac(31) = ...           = 2^26 * ...
fac(32) = ...           = 2^31 * ...
fac(33) = ...           = 2^31 * ...
fac(34) = ...           = 2^32 * ...  <===
fac(35) = ...           = 2^32 * ...
fac(36) = ...           = 2^34 * ...
...
fac(95) = ...           = 2^88 * ...
fac(96) = ...           = 2^93 * ...
fac(97) = ...           = 2^93 * ...
fac(98) = ...           = 2^94 * ...
fac(99) = ...           = 2^94 * ...
fac(100)= ...           = 2^96 * ...

の指数は、2他のすべての要因が奇数であるため、ベース2ビューの後続ゼロの数であり、したがって1、最後の2進数のaが積に寄与します。

同様のスキームは他の素数でも機能するため、次の因数分解を簡単に計算できますfac(100)

fac(100) = 2^96 * 3^48 * 5^24 * 7^16 * 11^9 * 13^7 * 17^5 * 19^5 * 23^4 *
           29^3 * 31^2 * 37^2 * 41^2 * 43^2 * 47^2 *
           53 * 59 * 61 * 67 * 71 * 73 * 79 * 83 * 89 * 97

したがって、コンピュータが基数3に数値を格納し、48桁の数値を持っている場合、fac(100)0になります(同様fac(99)ですが、そうでfac(98)はありません:-)

于 2011-03-15T21:35:10.700 に答える
6

良い問題-答えは次のとおりです。33の階乗(負の値による)-21474836480x80000000、または0xFFFFFFFF8000000064ビットを使用する場合です。34(次のメンバー)を掛けると、長い値が得られます。0xFFFFFFE600000000これをintにキャストすると、が得られます0x00000000

明らかに、その時点以降は0のままになります。

于 2011-03-15T20:47:40.233 に答える
3

再帰とBigIntegersを使用した単純なソリューション:

    public static BigInteger factorial(int num){
    if (num<=1)
        return BigInteger.ONE;
    else
        return factorial(num-1).multiply(BigInteger.valueOf(num));
    }

出力:

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
于 2015-11-22T02:20:43.263 に答える
0

ここにあり、質問に合うように少し調整されています)

public static void main(String[] args) {

    BigInteger fact = BigInteger.valueOf(1);
    for (int i = 1; i <= 100; i++)
        fact = fact.multiply(BigInteger.valueOf(i));
    System.out.println(fact);
}
于 2015-07-26T09:02:15.573 に答える
0

JavaのBigIntegerクラス。BigIntegerクラスは、使用可能なすべてのプリミティブデータ型の制限外にある非常に大きな整数の計算を含む数学演算に使用されます。

非常に大きな数を計算するには、BigIntegerを使用できます

たとえば、45の階乗を計算する場合は、回答= 119622220865480194561963161495657715064383733760000000000

 static void extraLongFactorials(int n) {
       BigInteger fact = BigInteger.ONE;
        for(int i=2; i<=n; i++){
            fact = fact.multiply(BigInteger.valueOf(i));
        }
        System.out.println(fact);
    }

BigIntegerの主なメソッドは、BigInteger.ONE、BigInteger.ZERO、BigInteger.TEN、BigInteger.ValueOf()です。

于 2018-10-03T07:42:00.677 に答える
0
import java.util.*;
import java.math.*;
public class BigInteger_Factorial {
    public static void main(String args []){
        Scanner s = new Scanner(System.in);

        BigInteger x,i,fac = new BigInteger("1");
        x = s.nextBigInteger();

        for(i=new BigInteger("1"); i.compareTo(x)<=0; i=i.add(BigInteger.ONE)){
            fac = fac.multiply((i));
        }
        System.out.println(fac);
    }
}

入力としての100の出力:

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

出力画像:

出力結果

于 2019-09-09T20:02:18.553 に答える
0
package test2;

import java.math.BigInteger;
import java.util.Scanner;

public class Factorial extends Big {

    public static void main(String args []){ 
    int x,fact=1,i ;
    Scanner sc = new Scanner(System.in);
    
    
    System.out.println("press any dight and 0 to exit");
    while (sc.nextInt()!=0)
    {
    System.out.println("Enter the values ");
    x=sc.nextInt();
    if(x<26)

    {
    for( i=1;i<=x;i++)
    {   fact = fact*i;  }
    
    System.out.println("Factorial of "+x + "is "+ fact );
    
    fact=1;
    }
    else 
    {
        System.out.println("In else big....");
    BigInteger k=fact(x);
    
    System.out.println("The factorial of "+x+"is "+k);
    System.out.println("RESULT LENGTH\n"+k.toString().length());
    }
    System.out.println("press any dight and 0 to exit");
    }
    System.out.println("thanks....");
    }
    
        
    
    
}
//----------------------------------------------------//

package test2;

import java.math.BigInteger;

public class Big {

    public static void main(String... args) {
        BigInteger fact = fact(100);
        System.out.println("fact(100) = " + fact);
        System.out.println("fact(100).longValue() = " + fact.longValue());
        System.out.println("fact(100).intValue() = " + fact.intValue());
        int powerOfTwoCount = 0;
        BigInteger two = BigInteger.valueOf(2);
        while (fact.compareTo(BigInteger.ZERO) > 0 && fact.mod(two).equals(BigInteger.ZERO)) {
            powerOfTwoCount++;
            fact = fact.divide(two);
        }
        System.out.println("fact(100) powers of two = " + powerOfTwoCount);
    }

    public static BigInteger fact(long n) {
        BigInteger result = BigInteger.ONE;
        for (long i = 2; i <= n; i++)
            result = result.multiply(BigInteger.valueOf(i));
        return result;
    }
    
}   
于 2020-10-13T05:54:54.100 に答える
-1

確かにオーバーフローです。doubleを試すことができます。64ビット長の整数はおそらく小さすぎます。

于 2011-03-15T20:45:12.347 に答える