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 | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 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