Skip to content

Instantly share code, notes, and snippets.

@NamPE286
Created December 26, 2022 03:50
Show Gist options
  • Save NamPE286/0791ffe402162853ec0987ab635905fa to your computer and use it in GitHub Desktop.
Save NamPE286/0791ffe402162853ec0987ab635905fa to your computer and use it in GitHub Desktop.
Count all coprime with N in range [1,N] (Euler's totient function)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll phi(ll n){
ll res = n;
for(ll i = 2; i * i <= n; i++){
if(n % i == 0){
while(n % i == 0) n /= i;
res -= res / i;
}
}
if(n != 1) res -= res / n;
return res;
}
int main(){
ll n;
cin >> n;
cout << phi(n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment