Skip to content

Instantly share code, notes, and snippets.

@soltrinox
Created December 18, 2014 15:57
Show Gist options
  • Save soltrinox/f4cc24da5e44f9b717d6 to your computer and use it in GitHub Desktop.
Save soltrinox/f4cc24da5e44f9b717d6 to your computer and use it in GitHub Desktop.
Totient - distance to prime
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