Last active
May 19, 2021 16:55
-
-
Save Diapolo10/e2537ca5b46f75825340d8dd4bee36f0 to your computer and use it in GitHub Desktop.
Python prime factor calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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