I have this prime factor generator for some number n. It returns list of all prime factors. In other words, product of the list equals n.
def prime_factors_generate(n):
a = []
prime_list = sorted(primes())
pindex = 0
p = prime_list[pindex]
num = n
while p != num:
if num % p == 0:
a.append(p)
num //= p
else:
pindex += 1
p = prime_list[pindex]
return a
The primes() function uses the Sundaram Sieve from herehere and returns a set of all primes below some limit, by default 106.
How can I make this code functional? I want to get rid of the while-loop, list appending and mutated state.