When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

vaibnak7's blog

By vaibnak7, history, 5 years ago, In English

Recently I gave Amazon coding test, in which i got the following question

Given n, find all numbers from 1 to n, which are coprime with n.
ex.
Input: n = 5
Output: 4
Explanation — The numbers coprime with n = 5 are (1,5), (2,5), (3,5), (4,5)

Input: n = 10
Output: 4
Explanation — (1,10), (3,10), (7,10), (9,10)

Constraints 1 <= n <= 10^9.

I wrote a brute force solution, where i took each numebr from 1 to n and tried to find its gcd with n. Which apparently gave a TLE on most of the test cases.

Can anyone kindly guide me how to solve this problem ?

  • Vote: I like it
  • +6
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by vaibnak7 (previous revision, new revision, compare).

»
5 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Basically you have to calculate $$$\phi(n)$$$. You can check this for detailed information. https://cp-algorithms.com/algebra/phi-function.html

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    ok, so means knowing euler totient function was a prerequisite to solve this question.

»
5 years ago, # |
  Vote: I like it +1 Vote: I do not like it

There's a cool function called Euler's totient function to calculate that.. this might help : https://en.wikipedia.org/wiki/Euler%27s_totient_function

»
5 years ago, # |
  Vote: I like it +6 Vote: I do not like it

Your problem could be solved by inclusion-exclusion principle(https://en.wikipedia.org/wiki/Inclusion–exclusion_principle ). It is easier to count numbers that are not coprime to n and not larger than and then substract the answer from n. These numbers are the numbers that share at least one prime divisor with n. To find out how many numbers with this property are in total we do the following: First factorize n in O(sqrtn). Now, knowing all its prime factors (let them be p1,p2,..pk) generate all the subsets of p1,p2,..pk (their number is bounded by the total numbers of divisors of n, so the complexity remains O(sqrtn)).For each subset, let the product of the numbers in it be P. If the subset has an odd number of elements, add n/P else subtract n/P from the answer. To understand why this is correct, think about how many numbers at most equal to n divide a number x, and try to understand how the inclusion exclusion principle works having the sets of numbers that divide each prime divisor of n, and trying to calculate the cardinality of their reunion. A solution that is less general, but can be justified by this approach and may run faster is to compute the Euler Totient function ( https://en.wikipedia.org/wiki/Euler%27s_totient_function ).

»
5 years ago, # |
Rev. 2   Vote: I like it +2 Vote: I do not like it

You can also solve it without any knowledge of euler totient using inclusion-exclusion. Coprime pairs = All numbers — (numbers multiple of p1 or numbers multiple of p2 or ...) where pi is the i-thm prime in the prime factorization of n. Ofc, this turns out to be the same thing as calculating the euler totient but you could solve it without knowing about it.

»
4 years ago, # |
Rev. 4   Vote: I like it +1 Vote: I do not like it

This a simple question of Euler's totient function

Let $$$n=p_1^{k_1}\cdot p_2^{k_2}\cdot ... \cdot p_n^{k_n}$$$ where $$$p_i$$$ is prime and $$$k_i \ge 1$$$.

The answer is $$$n\cdot \frac{p_1-1}{p_1}\cdot \frac{p_2-1}{p_2}\cdot ... \cdot \frac{p_n-1}{p_n}$$$

The proof is a hard to write but is easy to think of.

It can be done in at most $$$O(\sqrt n)$$$ time, not sure of the exact time complexity