Advertisement
JoelSjogren

phi cache

Aug 30th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. import collections
  2. import functools
  3. import operator
  4.  
  5. def prod(a, one=1):
  6.     return functools.reduce(operator.mul, a, one)
  7.  
  8. def many_phi(n):
  9.     facts = [collections.Counter() for _ in range(n)]
  10.     for i in range(2, n):
  11.         if not facts[i]:
  12.             k = i
  13.             while k < n:
  14.                 for j in range(k, n, k):
  15.                     facts[j][i] += 1
  16.                 k *= i
  17.     phi_facts = [sum((sum((facts[i] for _ in range(j-1)), collections.Counter())+facts[i-1]
  18.         for i, j in c.items()), collections.Counter()) for c in facts]
  19.     phis = [prod(j**k for j, k in i.items()) for i in phi_facts]
  20.     phis[0] = 0
  21.     return phis
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement