How to find number of values x which achieve GCD(n, x) = 1
n is constant
x <= m
m can be bigger than n
the related problem to this question that I am trying to solve:
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
How to find number of values x which achieve GCD(n, x) = 1
n is constant
x <= m
m can be bigger than n
the related problem to this question that I am trying to solve:
Name |
---|
a number $$$x$$$ having $$$gcd(n, x) = 1$$$ means that it doesn't have any common prime factors with $$$n$$$ so we basically need to count numbers from 1 to m not having any common prime factors with $$$n$$$.
We can solve the reverse problem which is counting the numbers from 1 to m having at least one common prime factor with $$$n$$$.
To do so we need to factorise $$$n$$$ then use inclusion-exclusion principle to count such integers. Let the prime factors be $$$[p_1, p_2, ..., p_k]$$$, first we are going to count numbers from 1 to m having $$$p_1$$$ as a prime factor or $$$p_2$$$ or $$$p_3$$$ or ... etc but if a number has both $$$p_1$$$ and $$$p_2$$$ then it was counted twice so we need to subtract all numbers having any combination of these or any other combination of 2 prime factors but it can be noticed again that if a number has $$$p_1$$$, $$$p_2$$$ and $$$p_3$$$ then it is counted 3 times then subtracted 3 times so we didn't count it at all so we need to count and add the numbers having all 3 prime factors together $$$p_1$$$, $$$p_2$$$ and $$$p_3$$$ and any other combination of 3 prime factors then subtract the ones having combination of 4 prime factors and add the ones having combination of 5 prime factors etc for the same reason.
Thank you, your explanation is very good