#!/usr/bin/python3 # -*- coding: utf-8 -*- import sys from typing import List def trial_division(n: int) -> List[int]: """Return a list of the prime factors for a natural number.""" # Prepare an empty list. factors: List[int] = [] # The first possible factor. possible_factor: int = 2 # While n still has remaining factors... while n > 1: # The remainder of n divided by f might be zero. if n % possible_factor == 0: # If so, it divides n. Add f to the list. factors.append(possible_factor) # Divide that factor out of n. n //= possible_factor # But if f is not a factor of n, else: # Add one to f and try again. possible_factor += 1 # Prime factors may be repeated: 12 factors to 2, 2, 3. return factors if __name__ == "__main__": print("Example trial division of 17: ", trial_division(17)) print("Example trial division of 16: ", trial_division(257)) print("Example trial division of 16: ", trial_division(16))