n = 1 から 1000 までの桁数の合計を 2^n で出力しようとしています。
public static void main(String[] args) {
int n = 1000;
for (int i = 1; i < n; i++) {
BigInteger power = BigInteger.valueOf((int)Math.pow(2, i));
int sum = 0;
while (power.intValue() > 0) {
sum += power.intValue() % 10;
power = power.divide(BigInteger.valueOf(10));
}
System.out.print(sum + " ");
}
}
約 2^30 程度までしか機能せず、残りは同じ結果の 46 を出力します。
Cで「ロングロング」を使用して同様のことを試みたところ、同様の制限の後に0が出力されました。
答えによると、私は変わりました
BigInteger power = BigInteger.valueOf((int)Math.pow(2, i));
に
BigInteger power = BigInteger.valueOf(2).pow(i);
46 が 0 に変更されました。C と同じです。まだ機能していません...