Advertisement
fdmartin

Untitled

Feb 26th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. from math import sqrt
  2.  
  3. limit = 10**7
  4. sieve = [1]*limit
  5. sieve[0] = 0
  6. sieve[1] = 0
  7. for i in range(0,   int(sqrt(limit))):
  8.     j = 2
  9.     if sieve[i] != 0:
  10.         while(i*j < limit):
  11.             sieve[i*j] = 0
  12.             j += 1
  13.  
  14. primes = [i for i in xrange(len(sieve)) if sieve[i]]
  15.  
  16. dic = {1 : []}
  17.  
  18. def prime_factorization(n):
  19.     divs = []
  20.     if n not in dic:
  21.         for i in primes:
  22.             if i > n: break
  23.             if n%i == 0:
  24.                 exp = 1
  25.                 while n%(i**exp)==0: exp += 1
  26.                 dic[n] = [[i, exp-1]] + prime_factorization(n/i**(exp-1))
  27.                 return dic[n]
  28.     return dic[n]
  29.  
  30. def totient(n):
  31.     tot = 1
  32.     for i in prime_factorization(n):
  33.         tot *= (i[0]**(i[1]-1))*(i[0]-1)
  34.     return tot
  35.  
  36. def is_permutation(x, y):
  37.     return sorted(str(x)) == sorted(str(y))
  38.  
  39. m = 20
  40. n = 0
  41.  
  42. for i in range(2,10**7):
  43.     if i%1000==0: print i
  44.     t = totient(i)
  45.     if is_permutation(t, i):
  46.         cur = float(i)/t
  47.         if cur < m:
  48.             print cur, t, i
  49.             m = cur
  50.             n = i
  51.  
  52. print m,n
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement