I'm still in the process of learning Python and am doing some of the Project Euler problems.
I have made a factorisation algorithm that works; however, it runs really slowly, and I'm not too well-versed on coding for performance. If someone could give me some pointers on how to improve the algorithm's efficiency it would be a huge help.
def factorise(num_to_factorize):
"""This method returns a list of factors for a particular number passed in to the parameters."""
factors = []
for i in range(1,math.floor(num_to_factorize/2)+1): # Loops from 1 to half the number, floors for odd numbers.
if num_to_factorize % i == 0: # Factor check.
factors.append(i) # Append on to an array to be checked.
factors.append(num_to_factorize) # Append the number itself as it is a factor.
return factors
def prime_factorise(num_to_prime_factorize):
"""This method returns the prime factors of the number passed in to the method as a parameter."""
prime_factors = []
for factor in factorise(num_to_prime_factorize): # Loop through each factor.
if factorise(factor) == [1, factor] or factorise(factor) == [1]: # if prime factors
prime_factors.append(factor)
return prime_factors