将阶乘与numpy或scipy数组结合使用时引起混淆的错误

问题描述 投票:0回答:1

以下代码标识素数列表

import math
import numpy as np
import pandas as pd
import scipy
for x in range(20, 50):
    print(x, math.factorial(x-1) % x)

输出为

20 0
21 0
22 0
23 22
24 0
25 0
26 0
27 0
28 0
29 28
30 0
31 30
32 0
33 0
34 0
35 0
36 0
37 36
38 0
39 0
40 0
41 40
42 0
43 42
44 0
45 0
46 0
47 46
48 0
49 0

每个余数的计算残差都不为零。当我尝试对数组进行相同的计算时,结果将有所不同。

arr = np.arange(20,50)
modFactArr = factorial(arr-1) % arr
print(np.column_stack((arr,modFactArr)),'\n\n')

smodFactArr=scipy.special.factorial(arr-1) % arr
print(np.column_stack((arr,smodFactArr)),'\n\n')

给予

[[20.  0.]
 [21.  0.]
 [22.  0.]
 [23. 22.]
 [24.  0.]
 [25. 22.]
 [26. 16.]
 [27. 11.]
 [28. 12.]
 [29. 16.]
 [30. 24.]
 [31.  8.]
 [32.  0.]
 [33. 18.]
 [34. 16.]
 [35. 33.]
 [36. 20.]
 [37. 12.]
 [38. 20.]
 [39. 10.]
 [40.  0.]
 [41. 25.]
 [42. 26.]
 [43.  6.]
 [44.  4.]
 [45.  3.]
 [46. 36.]
 [47. 40.]
 [48.  0.]
 [49. 12.]] 


[[20.  0.]
 [21.  0.]
 [22.  0.]
 [23. 22.]
 [24.  0.]
 [25. 22.]
 [26. 16.]
 [27. 11.]
 [28. 12.]
 [29. 16.]
 [30. 24.]
 [31.  8.]
 [32.  0.]
 [33. 18.]
 [34. 16.]
 [35. 33.]
 [36. 20.]
 [37. 12.]
 [38. 20.]
 [39. 10.]
 [40.  0.]
 [41. 25.]
 [42. 26.]
 [43.  6.]
 [44.  4.]
 [45.  3.]
 [46. 36.]
 [47. 40.]
 [48.  0.]
 [49. 12.]] 

现在注意,像26、27、28等这样的数字现在都在给出残差。这是我的代码错误吗?还是因为scipy和numpy进行模运算的原因不同?

numpy scipy factorial
1个回答
0
投票

由于np.dtype而发生。您只有np.int64溢出了,而本地python int却可以被bot溢出了。

© www.soinside.com 2019 - 2024. All rights reserved.