def gcd(A, B): A, B = int(A), int(B) steps = Steps() while B != A and B != 0: temp = B steps.add_mod() B = A % B A = temp return A, steps
def fermats_diff_of_squares_prewrapper(N): steps = Steps() factors = [] steps.add_mod() while N % 2 == 0: factors.append(2) N = N / 2 steps.add_mod() unfactored = [N] return factors, steps, unfactored
def harts_one_line(N, L=-1): steps = Steps() if L == -1: L = N for i in xrange(1, L): steps.add_sqrt(N * i) s = int(math.ceil(math.sqrt(N * i))) steps.add_mod() m = (s * s) % N isq, isq_steps = is_square(m) steps.append(isq_steps) if (isq): break steps.add_sqrt(m) t = math.sqrt(m) f1, gcd_steps = gcd(s - t, N) f2 = N / f1 steps.append(gcd_steps) return f1, f2, steps
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 trial_division(N, when_to_stop=None): primes_list = get_primes() m, pi, p = N, 0, 2 steps = Steps() factors = [] if when_to_stop == None: when_to_stop = math.sqrt(N) steps.add_sqrt(N) while (p <= when_to_stop): steps.add_mod() if m % p == 0: m = m / p factors.append(p) else: pi += 1 p = primes_list[pi] num_unfactored = 0 if m != 1: factors.append(m) if m not in primes_list: num_unfactored = 1 return factors, steps, num_unfactored