C# Common Prime Divisors
static int CommonPrimeDivisors(int[] a, int[] b)
{
long Gcd(long n, long m) => n % m == 0 ? m : Gcd(m, n % m);
long RemoveCommonPrimeDivisors(long n, long m)
{
while (n != 1)
{
var d = Gcd(n, m);
if (d == 1)
{
break;
}
n /= d;
}
return n;
}
var counter = 0;
for (int i = 0; i < a.Length; i++)
{
long x = a[i];
long y = b[i];
var d = Gcd(x, y);
x = RemoveCommonPrimeDivisors(x, d);
if (x != 1)
{
continue;
}
y = RemoveCommonPrimeDivisors(y, d);
if (y == 1)
{
counter++;
}
}
return counter;
}
This checks whether two numbers are built from the same prime factors by repeatedly dividing out their shared parts.