Given a large N, I need to iterate through all phi(k) such that 1 < k < N :
O(N logN)
Here's an efficient python generator. The caveat is that it doesn't yield the results in order. It is based on https://stackoverflow.com/a/10110008/412529 .
Memory complexity is O(log(N)) as it only has to store a list of prime factors for a single number at a time.
CPU complexity is just barely superlinear, something like O(N log log N).
def totientsbelow(N):
allprimes = primesbelow(N+1)
def rec(n, partialtot=1, min_p = 0):
for p in allprimes:
if p > n:
break
# avoid double solutions such as (6, [2,3]), and (6, [3,2])
if p < min_p: continue
yield (p, p-1, [p])
for t, tot2, r in rec(n//p, partialtot, min_p = p): # uses integer division
yield (t*p, tot2 * p if p == r[0] else tot2 * (p-1), [p] + r)
for n, t, factors in rec(N):
yield (n, t)