Given a sequence of positive integers, say a, count number of ordered distinct triplets such that their greatest common divisor is 1.
int primes[N] = {1} // 1 a prime
int lowestPrime[N]
int phi[N]
int mu[N]
for i in [2 .. N]:
if primes[i] is 1:
lowestPrime[i] = i
j = i
while j <= N:
primes[j] = 0
if primes[j] is 1:
lowestPrime[j] = i
primes[j] = 0
j += i
phi[i] = i - 1
mu[i] = -1
else:
if lowestPrime[i] is lowestPrime[i / lowestPrime[i]]:
mu[i] = 0
phi[i] = lowestPrime[i] * phi[i / lowestPrime[i]]
else:
mu[i] = -1 * mu[i / lowestPrime[i]]
phi[i] = (lowestPrime[i] - 1) * phi[i / lowestPrime[i]]
We can do all of the above in: