''' Created on 2013-01-29 @author: paymahn ''' ''' Use Eulers totient function: http://www.math.okstate.edu/~wrightd/crypt/lecnotes/node18.html ''' import CommonFunctions as cf MAX = 10**6 totient = [0]*(MAX+1) primes = cf.sieve_of_Erastothenes(MAX) for i in range(2,MAX+1): factors = cf.find_prime_factors(i, primes) product = 1 for factor in factors: product *= factor**factors[factor] - factor**(factors[factor]-1) totient[i] = product print sum(totient) * 3/7
Created on 2013-01-28 @author: paymahn The idea is to make a sieve and cross of multiples. All unique fractions are kept unmarked. ''' import CommonFunctions as cf from time import time t = time() MAX = 50 primes = set(cf.sieve_of_Erastothenes(MAX)) tally = 0 for denom in range(2,MAX + 1): if denom in primes: tally += denom - 1 else: if denom %2 == 0: jump = 2 else: jump = 1 for num in range(1,denom,jump): if cf.gcd(num, denom) == 1: tally += 1
import CommonFunctions as cf sieve = cf.sieve_of_Erastothenes(10000) best_num = 0 best_ratio = 0 for i in range(1,1000000): phi = cf.eulers_totient(i, sieve=sieve) if i / phi > best_ratio: best_ratio = i / phi best_num = i print best_num