Python Program to Find Prime Factors of a Number
In this post I am going to write a program in python that finds all the prime factors of a number. I am going to start by writing an empty function and a test. def get_prime_factors(number): prime_factors = [] return prime_factors if __name__ == "__main__": n = 8 expected = [2, 2, 2] result = get_prime_factors(n) assert expected == result, result Now, if you run the program above, it will give an AssertionError, as we are returning an empty list. Let's write some code to make our test pass. def get_prime_factors(number): prime_factors = [] while number % 2 == 0: prime_factors.append(2) number = number // 2 return prime_factors The program will work for multiple of 2's. Our next task is to find the other prime factors. For example, prime factors of 10 are 2 and 5. We shall add this test case first and then write code to pass this test....