本文主要介绍基本思路和具体的python代码实现
Pollard rho算法基本思路
1、F(x)=x^2+a 中的a可以试情况而定,一般取1,但尽量不要取0和2;
2、种子默认取2,原则上不小于2;
3、Pollard算法可以快速获取数n的几个因子,但这些因子不是n的质因子,也不是n的所有因子;
4、通过限定x>n , 规避循环问题。
使用Pollard rho算法求最小公倍数的基本思路
本方法只经过有限验证,不保证一定正确,仅做参考。
具体python2.7代码如下:
# !/user/bin/env python
# -*-coding: utf-8 -*-
'''
Using Pollard Pho factoring to find LCM(a, b)
seed = 2, f(x) = x^2 + 1
'''
from fractions import gcd
# find the factors of n with large power
def Pollard(n):
# generate the list(List_x) of x
x = 2
f = lambda x: x * x + 1
List_x = [2]
while x < n:
x = f(x)
List_x.append(x)
List_x = [x % n for x in List_x]
# calculate p = gcd(x(i) - x(j), n) and remove the same p
List_p = [gcd((List_x[i] - List_x[j]), n) for i in range(1, len(List_x)) for j in range(i)]
List_p = list(set(List_p))
List_p.sort()
# list factors of n
List_factor = [p for p in List_p if p > 1]
return List_factor
print 'Using Pollard Pho factoring to find LCM(a, b)'
print 'seed = 2, f(x) = x^2 + 1'
a = int(raw_input('input a:'))
b = int(raw_input('input b:'))
#obtain the factors of a and b, remove one of the same factor of both
prime_a = [a / i for i in Pollard(a)] + Pollard(a)
prime_b = [b / i for i in Pollard(b)] + Pollard(b)
temp = list(set(prime_a + prime_b))
temp.sort()
prime = [i for i in temp if i != 1]
#calculate LCM(a, b)
LCM = a * b
for i in prime:
while (LCM / i % a == 0) and (LCM / i % b == 0):
LCM = LCM / i
print '\nLCM(a, b) = %d\n' % LCM
raw_input('Press Enter to exit()')
本文介绍Pollard Rho算法的基本原理及其实现,并利用该算法求解两个整数的最小公倍数。包括算法核心步骤、Python代码实现及运行示例。
1338

被折叠的 条评论
为什么被折叠?



