Created
December 26, 2022 03:50
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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