砰的一声
有以下问题,我无法修好。 处理长度在5-52之间的数字,我不想得到它们的素因子。 使用Python,我发现了以下两种算法:
一
def faktorisiere(n):
l = [] # Lösungsmenge
# Auf Teilbarkeit durch 2, und alle ungeraden Zahlen von 3..n/2 testen
for i in chain([2], range(3, n//2 + 1, 2)):
# Ein Teiler kann mehrfach vorkommen (z.B. 4 = 2 * 2), deswegen:
while n % i == 0:
l.append(i)
print(i)
n = n // i # "//" ist ganzzahlige Division und entspricht int(n/i)
if i > n: # Alle Teiler gefunden? Dann Abbruch.
break
print(l)
二。
^{pr2}$但是第一个代码是非常慢的,像这样计算数字: 1917141215419419171412154194191714
第二个是工作速度快一点,但是我得到了错误的输出,我在代码中找不到错误。 我们以给定的数字为例。 以下是Python输出的第一行:
1917141215419419171412154194191714=1* 1917141215419419143900621852114944
1917141215419419171412154194191714 =2*958570607709709571950310926057472
但正如你所见,这些乘法并不等于结果。 密码有错误吗? 还是仅仅因为数字的长度? 希望你能帮助我。
致以最良好的祝愿, 计时工
你需要一个比试除法更好的算法来计算你正在处理的数字的大小。一个简单的步骤是从试验组升级到John Pollard的rho算法:
因子分解立即返回。请参见here以了解其工作原理的说明。要计算更大的数,您需要研究像椭圆曲线法或二次筛法这样的算法,但要注意这两种算法都很复杂。在
相关问题 更多 >
编程相关推荐