Created
December 18, 2014 15:57
-
-
Save soltrinox/f4cc24da5e44f9b717d6 to your computer and use it in GitHub Desktop.
Totient - distance to prime
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
public static int totient(int num){ //euler's totient function calculator. returns totient | |
int count=0; | |
for(int a=1;a<num;a++){ //definition of totient: the amount of numbers less than num coprime to it | |
if(GCD(num,a)==1){ //coprime | |
count++; | |
} | |
} | |
return(count); | |
} | |
public static int GCD(int a, int b){ //faster euclidean algorithm-see GCD for explanation | |
int temp; | |
if(a<b){ | |
temp=a; | |
a=b; | |
b=temp; | |
} | |
if(a%b==0){ | |
return(b); | |
} | |
return(GCD(a%b,b)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment