mutwo.utilities.prime_factors

Prime number calculations.

The functions “factorise” and “factors” are copied from the pyprimes library. In mutwo the function pyprimes.primes() has been replaced by primesieve.Iterator(), which improves speed by a factor of 10.

Functions:

factorise(integer)

param n

The number which shall be factorised.

factors(integer)

Yields tuples of (factor, count) where each factor is unique and usually prime, and count is an integer 1 or larger.

is_prime(n)

Test if number is prime or not.

factorise(integer) [list of factors][source]
Parameters

n (int) – The number which shall be factorised.

Returns

Returns a list of the (mostly) prime factors of integer n. For negative integers, -1 is included as a factor. If n is 0, 1 or -1, [n] is returned as the only factor. Otherwise all the factors will be prime.

Return type

List[int]

Example:

>>> factorise(-693)
[-1, 3, 3, 7, 11]
>>> factorise(55614)
[2, 3, 13, 23, 31]
factors(integer) yield factors of integer lazily[source]

Yields tuples of (factor, count) where each factor is unique and usually prime, and count is an integer 1 or larger. The factors are prime, except under the following circumstances: if the argument n is negative, -1 is included as a factor; if n is 0 or 1, it is given as the only factor. For all other integer n, all of the factors returned are prime.

Example:

>>> list(factors(3*7*7*7*11))
[(3, 1), (7, 3), (11, 1)]
Parameters

n (int) –

is_prime(n)[source]

Test if number is prime or not.

Parameters

n (int) – The number which shall be tested.

Returns

True if number is prime and False if number isn’t a Prime.

Return type

bool

(has been copied from here)