因子循环变为0

时间:2017-04-12 12:36:10

标签: algorithm factorial compiled-language

我使用编译语言运行一个简单的程序,使用两个简单的循环来计算前几个自然数的阶乘,一个用于跟踪我们计算阶乘的数字和一个计算内部数的外部循环通过将每个自然数从1乘以数字本身来得到阶乘。 该程序对于第一个自然数是完美的,然后大约从第13个值开始计算的因子显然是错误的。这是由于在现代计算机中实现的整数运算,我可以理解为什么会出现负值。 我不明白的原因是,为什么,这是我在不同的计算机上测试过的,经过非常少量的因子计算,它总是达到数字零。当然,如果第n个阶乘被评估为0,那么第(n + 1)个阶乘也将被评估为0,依此类推,但为什么数字0总是在非常少量的阶乘计算?

编辑:您可能想知道为什么我使用两个不同的周期而不是一个...我这样做是为了强制计算机从头开始重新计算每个因子,只是为了测试这个因素确实是因子总是0这不是偶然的。

这是我的输出:

Output of my program

2 个答案:

答案 0 :(得分:7)

从34!开始,所有因子都可以被2 ^ 32整除。因此,当您的计算机程序计算结果模2 ^ 32(虽然您不会说出您正在使用的编程语言,这很可能),但结果总是为0。

这是一个在Python中计算因子mod 2 ^ 32的程序:

def sint(r):
    r %= (1 << 32)
    return r if r < (1 << 31) else r - (1 << 32)

r = 1
for i in xrange(1, 40):
    r *= i
    print '%d! = %d mod 2^32' % (i, sint(r))

这给出了这个输出,它与你自己的程序的输出一致:

1! = 1 mod 2^32
2! = 2 mod 2^32
3! = 6 mod 2^32
4! = 24 mod 2^32
5! = 120 mod 2^32
6! = 720 mod 2^32
7! = 5040 mod 2^32
8! = 40320 mod 2^32
9! = 362880 mod 2^32
10! = 3628800 mod 2^32
11! = 39916800 mod 2^32
12! = 479001600 mod 2^32
13! = 1932053504 mod 2^32
14! = 1278945280 mod 2^32
15! = 2004310016 mod 2^32
16! = 2004189184 mod 2^32
17! = -288522240 mod 2^32
18! = -898433024 mod 2^32
19! = 109641728 mod 2^32
20! = -2102132736 mod 2^32
21! = -1195114496 mod 2^32
22! = -522715136 mod 2^32
23! = 862453760 mod 2^32
24! = -775946240 mod 2^32
25! = 2076180480 mod 2^32
26! = -1853882368 mod 2^32
27! = 1484783616 mod 2^32
28! = -1375731712 mod 2^32
29! = -1241513984 mod 2^32
30! = 1409286144 mod 2^32
31! = 738197504 mod 2^32
32! = -2147483648 mod 2^32
33! = -2147483648 mod 2^32
34! = 0 mod 2^32
35! = 0 mod 2^32
36! = 0 mod 2^32
37! = 0 mod 2^32
38! = 0 mod 2^32
39! = 0 mod 2^32

此处列出了这一系数因子的确切值,显示每个因子包含多少幂:

1! = 1. Divisible by 2^0
2! = 2. Divisible by 2^1
3! = 6. Divisible by 2^1
4! = 24. Divisible by 2^3
5! = 120. Divisible by 2^3
6! = 720. Divisible by 2^4
7! = 5040. Divisible by 2^4
8! = 40320. Divisible by 2^7
9! = 362880. Divisible by 2^7
10! = 3628800. Divisible by 2^8
11! = 39916800. Divisible by 2^8
12! = 479001600. Divisible by 2^10
13! = 6227020800. Divisible by 2^10
14! = 87178291200. Divisible by 2^11
15! = 1307674368000. Divisible by 2^11
16! = 20922789888000. Divisible by 2^15
17! = 355687428096000. Divisible by 2^15
18! = 6402373705728000. Divisible by 2^16
19! = 121645100408832000. Divisible by 2^16
20! = 2432902008176640000. Divisible by 2^18
21! = 51090942171709440000. Divisible by 2^18
22! = 1124000727777607680000. Divisible by 2^19
23! = 25852016738884976640000. Divisible by 2^19
24! = 620448401733239439360000. Divisible by 2^22
25! = 15511210043330985984000000. Divisible by 2^22
26! = 403291461126605635584000000. Divisible by 2^23
27! = 10888869450418352160768000000. Divisible by 2^23
28! = 304888344611713860501504000000. Divisible by 2^25
29! = 8841761993739701954543616000000. Divisible by 2^25
30! = 265252859812191058636308480000000. Divisible by 2^26
31! = 8222838654177922817725562880000000. Divisible by 2^26
32! = 263130836933693530167218012160000000. Divisible by 2^31
33! = 8683317618811886495518194401280000000. Divisible by 2^31
34! = 295232799039604140847618609643520000000. Divisible by 2^32
35! = 10333147966386144929666651337523200000000. Divisible by 2^32
36! = 371993326789901217467999448150835200000000. Divisible by 2^34
37! = 13763753091226345046315979581580902400000000. Divisible by 2^34
38! = 523022617466601111760007224100074291200000000. Divisible by 2^35
39! = 20397882081197443358640281739902897356800000000. Divisible by 2^35

答案 1 :(得分:2)

每次乘法都会向右追加零位,直到在某些迭代时,由于溢出,大多数位都会被丢弃。 行动效果:

    int i, x=1;
    for (i=1; i <=50; i++) {
        x *= i;
        for (int i = 31; i >= 0; --i) {
            printf("%i",(x >> i) & 1);
        }
        printf("\n");
    }

输出位:

00000000000000000000000000000001
00000000000000000000000000000010
00000000000000000000000000000110
00000000000000000000000000011000
00000000000000000000000001111000
00000000000000000000001011010000
00000000000000000001001110110000
00000000000000001001110110000000
00000000000001011000100110000000
00000000001101110101111100000000
00000010011000010001010100000000
00011100100011001111110000000000
01110011001010001100110000000000
01001100001110110010100000000000
01110111011101110101100000000000
01110111011101011000000000000000
11101110110011011000000000000000
11001010011100110000000000000000
00000110100010010000000000000000
10000010101101000000000000000000
10111000110001000000000000000000
11100000110110000000000000000000
00110011100000000000000000000000
11010000000000000000000000000000
10000000000000000000000000000000
00000000000000000000000000000000

请注意,在我们得到零之前 - 我们得到 INT_MIN 。附加另一个零位 - 丢弃符号位,因此从INT_MIN中我们得到纯零。