Calculating phi(k) for 1<k<N

前端 未结 9 1865
花落未央
花落未央 2020-12-04 20:08

Given a large N, I need to iterate through all phi(k) such that 1 < k < N :

  • time-complexity must be O(N logN)
  • memory-complexity mus
9条回答
  •  佛祖请我去吃肉
    2020-12-04 21:04

    I think you can go the other way around. Instead of factorizing an integer k to get phi(k), you can attempt to generate all integers from 1 to N from primes and get phi(k) quickly. For example, if Pn is the maximum prime that is less than N, you can generate all integers less than N by

    P1 i 1 * P2 i 2 * ... * Pn i n where each ij run from 0 to [log (N) / log (Pj)] ([] is the floor function).

    That way, you can get phi(k) quickly wihout expensive prime factorization and still iterate through all k between 1 and N (not in order but I think you don't care about order).

提交回复
热议问题