You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.2 KiB
Java

package com.jwetherell.algorithms.mathematics;
/**
* In number theory, two integers a and b are said to be relatively prime, mutually prime, or coprime (also spelled
* co-prime)[1] if the only positive integer that divides both of them is 1. That is, the only common positive factor
* of the two numbers is 1. This is equivalent to their greatest common divisor being 1.
* <p>
* http://en.wikipedia.org/wiki/Coprime_integers
* <br>
* @author Szymon Stankiewicz <mail@stankiewicz.me>
* @author Justin Wetherell <phishman3579@gmail.com>
*/
public class Coprimes {
private Coprimes() { }
/**
*
* Euler's totient function. Because this function is multiplicative such implementation is possible.
* <p>
* Time complexity: O(sqrt(n))
* <p>
* @param n Long integer
* @return number of coprimes smaller or equal to n
*/
public static long getNumberOfCoprimes(long n) {
if(n < 1)
return 0;
long res = 1;
for(int i = 2; i*i <= n; i++) {
int times = 0;
while(n%i == 0) {
res *= (times > 0 ? i : i-1);
n /= i;
times++;
}
}
if(n > 1)
res *= n-1;
return res;
}
}