A small prime factorization program that can be called with a number:
In case you're in the mood for extending this functionality, the file also offers two generators: primes
and factorize
.
from factorize import primes, factorize
primes
is a simple generator for creating prime numbers.
for prime in primes():
print(prime)
# prints 2, 3, 5, 7, ...
Optionally, you can provide a maximum number n
of primes you want to get:
print(list(primes(5)))
# prints [2, 3, 5, 7, 11]
Or declare a limit
for the largest prime (primes you get <= limit
):
print(list(primes(limit=100)))
# prints [2, 3, ..., 89, 97]
factorize
is a generator that lets you factorize a number into its primes.
for factor in factorize(2595925957847039):
print(factor)
# prints 38047, 140281, and 486377 with a break between each number as it takes
# some time to calculate each factor
The generator yields factors as they become available.