当使用 Java 使用整数计算 100 (100!) 的阶乘时,我得到 0
这样做时:
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);
}
我得到this。
When doing this:
int x = 100;
int result = 1;
for (int i = 1; i < (x + 1); i++) {
result = (result * i);
}
System.out.println(result);
This is clearly because the result is too big for an integer, but I am used to get big negative numbers for the overflow, and not 0.
Thanks in advance!
When I switch to this:
int x = 100;
int result = 1;
for (int i = 1; i < (x + 1); i++) {
result = (result * i);
System.out.println(result);
}
I get this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
1到100之间有50个偶数。这意味着阶乘是 2 的倍数至少 50 次,换句话说,作为二进制数,最后 50 位将是 0。(实际上更重要,因为偶数第二个偶数是 2*2 的倍数等
)
这意味着对于fact(100) 的最低位来说,97 位整数将为0。
事实上,对于fact(n) 来说,2 的幂数非常接近n。事实上(10000)有 9995 个 2 的幂。这是因为它大约是 1/2 的 n 次幂之和,总计接近
n
。即每第二个数字都是偶数 n/2,每 4 个数字都有一个额外的 2 次幂 (+n/4),每一个第 8 个数字有一个额外的幂 (+n/8) 等等,接近n
作为总和。There are 50 even numbers between 1 and 100 inclusive. This means that the factorial is a multiple of 2 at least 50 times, in other words as a binary number the last 50 bits will be 0. (Actually it is more as even second even number is a multiple of 2*2 etc)
prints
This means a 97-bit integer would be 0 for the lowest bits of fact(100)
In fact, the number of powers of two is very close to n for fact(n). For fact(10000) there are 9995 powers of two. This is because its is approximately the sum of n times powers of 1/2 giving a total close to
n
. i.e. every second number is even n/2 and every 4th has an additional power of 2 (+n/4) and every 8th has an additional power (+n/8) etc approachesn
as a sum.大负数是溢出到特定范围的值;
factorial(100)
末尾有超过 32 个二进制零,因此将其转换为整数会产生零。Big negative numbers are values that overflowed into certain ranges;
factorial(100)
has more than 32 binary zeros on the end, so converting it to an integer produces zero.为了了解原因,我们可以观察阶乘的素因式分解。
2
的指数是基数 2 视图中尾随零的数量,因为所有其他因子都是奇数,因此在最后一个二进制数字中贡献一个1
产品。类似的方案也适用于其他素数,因此我们可以轻松计算 fac(100) 的因式分解:
因此,如果我们的计算机以 3 为基数存储数字,并且有 48 个 trit-numbers ,
fac(100)
将为 0(也如fac(99)
,但fac(98)
不会:-)To have a look at the cause, we could observe the prime factorization of the factorial.
The exponent for the
2
is the number of trailing zeros in the base-2 view, as all other factors are odd, and thus contribute a1
in the last binary digit to the product.A similar scheme works for other prime numbers, too, so we can easily calculate the factorization of
fac(100)
:So, if our computer stored the numbers in base 3, and had 48-trit-numbers,
fac(100)
would be 0 (asfac(99)
, too, butfac(98)
would not :-)好问题 - 答案是:
33 的阶乘(由于负值)为
-2147483648
,即0x80000000
,如果采用 64 位,则为0xFFFFFFFF80000000
。乘以 34(下一个成员)将得到一个 long 值0xFFFFFFE600000000
,当转换为 int 时将得到0x00000000
。显然从那时起你将保持 0。
Nice problem - answer is:
Factorial of 33 (due to negative values) is
-2147483648
which is0x80000000
, or0xFFFFFFFF80000000
if taking 64bits. Multiplying by 34 (the next member) will give a long value of0xFFFFFFE600000000
, which when casting to int will give you0x00000000
.Obviously from that point onwards you will remain with 0.
使用递归和 BigIntegers 的简单解决方案:
输出:
Simple solution using recursion and BigIntegers:
Output:
(在此处找到,稍微修改以适应问题)
(Found here, adapted slightly to fit question)
Java 中的 BigInteger 类。 BigInteger 类用于数学运算,涉及超出所有可用原始数据类型限制的非常大的整数计算。
要计算非常大的数字,我们可以使用BigInteger
BigInteger的主要方法是BigInteger.ONE,BigInteger.ZERO,BigInteger.TEN,BigInteger.ValueOf()
BigInteger Class in Java. BigInteger class is used for mathematical operation which involves very big integer calculations that are outside the limit of all available primitive data types.
To calculate very large number, we can use BigInteger
Main methods of BigInteger is BigInteger.ONE, BigInteger.ZERO, BigInteger.TEN, BigInteger.ValueOf()
输出 100 作为输入:
输出图像:
Output of 100 as input:
Output image:
这肯定是溢出,你可以尝试双精度,64位长整数可能太小
it's an overflow for sure, you can try double, 64 bit long integer are probably too small