$\mathfrak{Mini}$ $\mathbb{Wiki}$
Please remove syntax.php from lib/plugins/nodisp or remove the plugin and reinstall

1. June 2020 mozgan   

Prime Factorization

This algorithm gives a list in which the all primes multiply together to make the given original number $n$.

#!/usr/bin/python
 
import math
 
def primes(n):
    p = list()
 
    while n % 2 == 0:
        p.append(2)
        n>>= 1
 
    for i in range(3, int(math.sqrt(n))+1, 2):
        while n % i == 0:
            p.append(i)
            n = n // i
 
    # if n is a prime
    if n > 2:
        p.append(n)
 
    return p

Example:

print(primes(127493291))

The primes of $127493291$ are $[193, 647, 1021]$.

print(primes(864))

The primes of $864$ are $[2, 2, 2, 2, 2, 3, 3, 3]$.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies