Skip to content

Instantly share code, notes, and snippets.

@potix2
Created October 10, 2011 07:49
Show Gist options
  • Save potix2/1274819 to your computer and use it in GitHub Desktop.
Save potix2/1274819 to your computer and use it in GitHub Desktop.
totient
//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