def coprime(a, b): """Are integers a and b coprime?""" primes_a = pfactor(a) primes_b = pfactor(b) primes = [p for p in primes_a if p in primes_b] return bool(primes)
def factor(n): primes = pfactor(n) factors = [1, n] pow = {} for p in primes: if p not in pow.keys(): pow[p] = 0 pow[p] = pow[p] + 1 factors.append(p**pow[p]) for p in [f for f in factors if f > 1]: f = n / p if f not in factors: factors.append(n / p) combos = unique_combinations(factors, 2) for c in combos: f = product(c) if f < n and n % f == 0 and f not in factors: factors.append(f) if n not in factors: factors.append(n) return sorted(set(factors))