Created
October 10, 2011 07:49
-
-
Save potix2/1274819 to your computer and use it in GitHub Desktop.
totient
This file contains hidden or 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
//http://mathworld.wolfram.com/TotientFunction.html | |
//use (13) and (14) | |
int totient(int n) { | |
int ret = 1; | |
for ( int i = 2; n != 1; i++ ) { | |
if ( n % i == 0 ) { | |
int pow = 1; | |
while ( n % i == 0 ) { | |
n = n / i; | |
pow *= i; | |
} | |
ret = ret * pow / i * (i - 1); | |
} | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment