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 ?
Auto comment: topic has been updated by vaibnak7 (previous revision, new revision, compare).
Basically you have to calculate $$$\phi(n)$$$. You can check this for detailed information. https://cp-algorithms.com/algebra/phi-function.html
ok, so means knowing euler totient function was a prerequisite to solve this question.
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
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 bep1,p2,..pk
) generate all the subsets ofp1,p2,..pk
(their number is bounded by the total numbers of divisors of n, so the complexity remainsO(sqrtn)
).For each subset, let the product of the numbers in it be P. If the subset has an odd number of elements, addn/P
else subtractn/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 ).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.
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