Last active
March 14, 2020 03:26
-
-
Save Tdrj2716/d0ddb36efeca5f7a18029eed336a5643 to your computer and use it in GitHub Desktop.
最大公約数、最小公倍数、桁和といった簡単な関数群
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
int gcd(int a, int b) { | |
return b ? gcd(b, a%b) : a; | |
} | |
int lcm(int a, int b) { | |
return a / gcd(a, b) * b; | |
} | |
int digsum(int n) { | |
int res = 0; | |
while(n > 0) { | |
res += n % 10; | |
n /= 10; | |
} | |
return res; | |
} | |
int phi(unsigned int n){ | |
if(n == 1) return 1; | |
int r = n; | |
for(int i = 2; i <= n; i++){ | |
if(!(n % i)){ | |
r = r * (i - 1) / i; | |
while (!(n % i)){ | |
n /= i; | |
} | |
} | |
} | |
return r; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment