def main(): primes.init(1000*1000+10) cur = 2 total = 0 while cur <= 1000*1000: total+= int(primes.euler_phi(cur)) if cur % 1000 == 0: print cur if total < 0: print type(total),type(cur) break cur +=1 print total
def brute_force(): cands = range(10**6,10**7) for i,x in enumerate(cands): ep = primes.euler_phi(x) cands[i] = x,ep,x/(1.*ep) print "eulerphi computed" cands.sort(key=lambda x:x[2]) print "sorted" for cand in cands: if cfn.digit_hash(cand[0])==cfn.digit_hash(cand[1]): print cand break
def hyperexponent_mod(n,k,mod): stack = [] acc = n while k != 1: phi = primes.euler_phi(mod,ignore=True) stack.append((phi,mod)) mod = phi k-=1 stack.reverse() #print stack for phi,mod in stack: acc = pow(n,acc%phi,phi) return acc%mod
def smallest_k(n,min=0): if gcd(n,10) != 1: return None if gcd(n-9,n) != 1: return None # return handle_zero_divisors(n) factors = primes.all_factors(primes.factor(primes.euler_phi(n))) if factors[-1] < min: return None for factor in factors: factor = int(factor) if pow(10,factor,n) == 1: if factor < min: return None return factor
def handle_zero_divisors(n): print "Zero divisors", factors = primes.all_factors(primes.factor(primes.euler_phi(n))) possibilities = PriorityQueue() for factor in factors: factor = int(factor) if pow(10,factor,n) == 1: possibilities.put((factor,factor,1)) if possibilities.empty(): return None while True: #should eventually terminate _,factor,multiple = possibilities.get() if pow(100,factor,n) == 1: print n,factor*multiple return factor*multiple possibilities.put((factor*(multiple*1),factor,multiple+1))
def hyperexponent_mod_rec(n,k,mod): if k ==1: return n%mod phi = primes.euler_phi(mod,ignore=True) return pow(n,hyperexponent_mod_rec(n,k-1,mod),mod)%mod