Skip to content

Prime factorization

Python division loop

Flowchart (ISO 5807)

YesYesNoNoYesNoStartInput nd = 2d² ≤ nn mod d = 0Output dn = n div dd = d + 1n > 1Output nEndFigure 1 — factorize

Source code

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

The editor opens with this code — change it and the chart redraws.

Language
Python
Constructs
division loop
Topic
Algorithms
Formats
Word, Visio, draw.io, PDF, SVG

Related examples