← All examples

Prime factorization

Python Algorithms

division loop

Flowchart (ISO 5807)

YesYesNoNoYesNoStartInput nd = 2d * d <= nn % d == 0Output dn //= dd += 1n > 1Output nEndFigure 1 — factorize

Source code

def factorize(n):
    d = 2
    while d * d <= n:
        while n % d == 0:
            print(d)
            n //= d
        d += 1
    if n > 1:
        print(n)