galois.prime_factors¶
- galois.prime_factors(n)¶
Computes the prime factors of the positive integer \(n\).
The integer \(n\) can be factored into \(n = p_1^{e_1} p_2^{e_2} \dots p_{k-1}^{e_{k-1}}\).
Steps:
Test if \(n\) is prime. If so, return
[n], [1]
.Test if \(n\) is a perfect power, such that \(n = x^k\). If so, prime factor \(x\) and multiply its exponents by \(k\).
Use trial division with a list of primes up to \(10^6\). If no residual factors, return the discovered prime factors.
Use Pollard’s Rho algorithm to find a non-trivial factor of the residual. Continue until all are found.
- Parameters
n (int) – The positive integer to be factored.
- Returns
list – Sorted list of \(k\) prime factors \(p = [p_1, p_2, \dots, p_{k-1}]\) with \(p_1 < p_2 < \dots < p_{k-1}\).
list – List of corresponding prime powers \(e = [e_1, e_2, \dots, e_{k-1}]\).
Examples
In [1]: p, e = galois.prime_factors(120) In [2]: p, e Out[2]: ([2, 3, 5], [3, 1, 1]) # The product of the prime powers is the factored integer In [3]: np.multiply.reduce(np.array(p) ** np.array(e)) Out[3]: 120
Prime factorization of 1 less than a large prime.
In [4]: prime =1000000000000000035000061 In [5]: galois.is_prime(prime) Out[5]: True In [6]: p, e = galois.prime_factors(prime - 1) In [7]: p, e Out[7]: ([2, 3, 5, 17, 19, 112850813, 457237177399], [2, 1, 1, 1, 1, 1, 1]) In [8]: np.multiply.reduce(np.array(p) ** np.array(e)) Out[8]: 2003764205241896700