Well i did it from scratch , It uses a modification of the prime number sieve algorithm .
from math import sqrt
from collections import Counter
def prime_factors(lst):
maximumnumber=max(lst)
primesieve=[0]*(maximumnumber+1)
primesieve[0]=1
primesieve[1]=1
#DOING PRIME NUMBER SIEVE
for i in range(2,int(sqrt(maximumnumber))+1):
if(primesieve[i]==0):
for j in range(2*i,len(primesieve),i):
primesieve[j]=i
for j in range(len(lst)):
num=lst[j]
factors=[]
#take each number(num) and divided it by a prime number (primesieve[num])
#do it until you find no more prime factors for number
while(primesieve[num]>0):
factors.append(primesieve[num])
num=num//primesieve[num]
factors.append(num)
yield Counter(factors)
for i in prime_factors([3,4,6]):
print(i)
Output
Counter({3: 1})
Counter({2: 2})
Counter({2: 1, 3: 1})
Ill explain what i have done
- Found prime numbers between the 0 and maximum element in the list using the prime number sieve algorithm , Link to a the algorithm , I just modified one part that algorithm that is instead of using a the primenumber array as boolean i used ,Integer to show which number it was divided with
- Iterate through the numbers and find all the prime factors