""" Created on May 23, 2014 @author: anuvrat """ from utils import prime_utils LIMIT = 10000000 def check_condition(num): t = prime_utils.totient(num) return num * 1.0 / t if sorted(str(num)) == sorted(str(t)) else -1 primes = list(prime_utils.primesfrom2to(5000)) primes.reverse() smallest_ratio = LIMIT smallest_num = LIMIT for a in primes: max_allowed = LIMIT / a for b in (i for i in primes if i <= max_allowed and i > 1000): n = a * b phi = n - a if a == b else n - (a + b) + 1 if not sorted(str(n)) == sorted(str(phi)): continue ratio = n * 1.0 / phi if ratio < smallest_ratio:
''' Created on Sep 28, 2012 @author: anuvrat ''' from utils.prime_utils import totient, primesfrom2to from utils.memoize import Memoize @Memoize def totient_chain_length( n ): if n in range( 21 ): return ( 1, 1, 2, 3, 3, 4, 3, 4, 4, 4, 4, 5, 4, 5, 4, 5, 5, 6, 4, 5, 5 )[n] return 1 + totient_chain_length( totient( n ) ) total = 0 for prime in primesfrom2to( 40000000 ): if prime < 9000000: continue if totient_chain_length( prime - 1 ) == 24: total += prime print( total )