Python Common Prime Divisors
from math import gcd
def common_prime_divisors(a: list[int], b: list[int]) -> int:
def remove_common_prime_divisors(n: int, m: int) -> int:
while n != 1:
d = gcd(n, m)
if d == 1:
break
n //= d
return n
counter = 0
for x, y in zip(a, b):
d = gcd(x, y)
x = remove_common_prime_divisors(x, d)
if x != 1:
continue
y = remove_common_prime_divisors(y, d)
if y == 1:
counter += 1
return counter
This checks whether two numbers are built from the same prime factors by repeatedly dividing out their shared parts.