import java.math.BigInteger;
import java.util.ArrayList;
public class Factorial {
public static int[] bitVector(int n) {
ArrayList<Integer> bitList = new ArrayList<Integer>();
BigInteger input = computeFactorial(n);
System.out.println(input);
BigInteger[] result = input.divideAndRemainder(new BigInteger(String.valueOf(2)));
if (result[0].intValue()==0) {return new int[]{result[1].intValue()};}
else {
bitList.add(result[1].intValue());
}
while(result[0].intValue() != 0) {
result = result[0].divideAndRemainder(new BigInteger(String.valueOf(2)));
bitList.add(result[1].intValue());
}
int[] array = new int[bitList.size()];
for (int i=0; i<array.length; i++) {
array[i]=bitList.get(i).intValue();
}
return array;
}
public static BigInteger computeFactorial(int n) {
if (n==0) {
return new BigInteger(String.valueOf(1));
} else {
return new BigInteger(String.valueOf(n)).multiply(computeFactorial(n-1));
}
}
public static void main(String[] args) {
int[] bitVector = bitVector(35);
for (int bit: bitVector)
System.out.print(bit+" ");
System.out.println();
}
}
上記のコードは、への入力が。以下の場合に正常に機能しbitVector
ます35
。36
ただし、パラメータとしてに渡すとbitVector
、1ビットを除くすべてが出力に含まれなくなります。
私は潜在的に次の原因を除外しました:
BigIntegerタイプはオーバーフローしないように設計されているため、BigIntegerタイプとは関係がない可能性があります。
380M
実行時にのみ使用するプログラムのメモリ使用量とは関係ない場合があります。の値を出力します
computeFactorial(36)
。これはよさそうです。
いったい何が起こっているのでしょうか?