Skip to content

Instantly share code, notes, and snippets.

@Diapolo10
Last active May 19, 2021 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Diapolo10/e2537ca5b46f75825340d8dd4bee36f0 to your computer and use it in GitHub Desktop.
Save Diapolo10/e2537ca5b46f75825340d8dd4bee36f0 to your computer and use it in GitHub Desktop.
Python prime factor calculator
from typing import List
def prime_factors(num: int) -> List[int]:
"""
Searches for numbers that evenly divide the given
number and makes a list of these numbers, returning
the list. The product of the list is equivalent to
the original number as long as it's > 1, returning
an empty list otherwise.
"""
factors = []
while 1 < num:
for factor in range(2 if not factors else factors[-1], num+1):
if num % factor == 0:
factors.append(factor)
num //= factor
break
return factors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment