def f(a, b, p): if b == 1 or p == 2: return a % p x = f(a, b-1, phi(p)) return pow_mod(a, x, p)
#https://github.com/skydark/project-euler/blob/master/129.py from time import time; t=time() from mathplus import pow_mod, factorization, phi M = 1000000 S = M//2*2 + 1 T = 1001000 def report(p): print(p)#, time()-t) import sys sys.exit() for p in range(S, T, 2): if p % 5 == 0: continue phip = phi(p) m = p if p % 3 == 0: phip *= 9 m *= 9 if phip <= M: continue factors = factorization(phip) while phip > M: for f, c in factors: phif = phip//f if pow_mod(10, phif, m) == 1: phip = phif if c > 1: factors = [(k, v) if k != f else (k, v-1) for k, v in factors] else:
def f(a, b, p): if b == 1 or p == 2: return a % p x = f(a, b - 1, phi(p)) return pow_mod(a, x, p)