MarcelGarus / factorize

A simple real-time prime factorization program for the command line. Also supports imports as a python library with generators for primes and prime factors.
1 stars 1 forks source link

factorize

A small prime factorization program that can be called with a number:

python factorize.py 2595925957847039

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

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

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.