def pollards_pminus1(N, B=None, steps=None): if steps == None: steps = Steps() if B == None: steps.add_cuberoot(N) B = math.pow(N, 1.0 / 3.0) primes_list = get_primes() a, i = 2, 0 a_set = set([]) while True: pi = primes_list[i] if pi > B: break steps.add_log(B) steps.add_log(pi) e = int(math.floor(math.log(B) / math.log(pi))) steps.add_exp(pi, e) f = pi**e steps.add_exp(a, f) steps.add_mod() a_hold = powering.power_mod(a, f, N) #(a**f) % N if a_hold == 0: break a = a_hold a_set.add(a) i += 1 for a in a_set: g, g_steps = gcd(a - 1, N) steps.append(g_steps) if 1 < g and g < N: return g, N / g, steps return None, None, steps
def lehmans_var_of_fermat(N): s3n = int(math.ceil(N**(1.0 / 3.0))) steps = Steps() for k in xrange(1, s3n + 1): steps.add_sqrt(k * N) sk = 2.0 * math.sqrt(k * N) steps.add_exp(sk + N, 1.0 / 6.0) steps.add_sqrt(k) for a in xrange( int(math.ceil(sk)), int( math.floor(sk + N**(1.0 / 6.0) / (4.0 * math.sqrt(k))) + 1)): b = a * a - 4 * k * N isq, isq_steps = is_square(b) steps.append(isq_steps) if isq: my_gcd, gcd_steps = gcd(a + math.sqrt(b), N) steps.append(gcd_steps) return my_gcd, N / my_gcd, steps raise Exception("should not get here ")