Fasho's blog

By Fasho, history, 2 years ago, In English

Given a number x and a number y how can I count the number of relatively prime numbers with x that are less then y?

More Formally;

Is there any way to count how many z exists that:(1<=z<=y and gcd(x,z)==1) (x<=4005,y<=10^9).

Sorry for my bad England.

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

»
2 years ago, # |
  Vote: I like it +4 Vote: I do not like it

take all distinct prime numbers that divide x, and apply inclusion-exclusion principle, it works in O(2^number_of_distinct_prime_divisors_of_x), i wont explain inclusion-exclusion here because it is already explained on the internet so ill give u a link to it https://cp-algorithms.com/combinatorics/inclusion-exclusion.html, and u also have your question fully explained on that page i gave u

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

    Thanks a lot but for my question I need a solution like O(1) so I wonder if there is a math formula for this problem.

»
2 years ago, # |
  Vote: I like it +7 Vote: I do not like it

Since $$$\gcd(x,z)=\gcd(x+z,z)$$$, we can just calculate $$$c[x][y]=\sum\limits_{i=1}^{y}[\gcd(i,x)=1](0\le y<x\le 4005)$$$, the answer is $$$\lfloor\frac{y}{x}\rfloor c[x][x-1]+c[x][y\bmod x]$$$(or something similar).

»
2 years ago, # |
  Vote: I like it +40 Vote: I do not like it

What is Sorry for my bad England :)