Go Common Prime Divisors
func commonPrimeDivisors(a, b []int) int {
var gcd func(n, m int) int
gcd = func(n, m int) int {
if n%m == 0 {
return m
}
return gcd(m, n%m)
}
removeCommonPrimeDivisors := func(n, m int) int {
for n != 1 {
d := gcd(n, m)
if d == 1 {
break
}
n /= d
}
return n
}
counter := 0
for i := range a {
x, y := a[i], b[i]
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.