- Algorithm to slightly modify [Eratosthenes' sieve](/en/Eratosthenes'%20sieve) to allow for [prime factor decomposition](/en/prime%20factor%20decomposition) without trial splitting
def precompute(AS)
maxAS = max(AS)
eratree = [0] * (maxAS + 10)
for p in range(2, maxAS + 1):
if eratree[p]:
continue
# p is prime
eratree[p] = p
x = p * p
while x <= maxAS:
if not eratree[x]:
eratree[x] = p
x += p
return eratree
x = p * pfor a in AS:
factors = []
while a > 1:
d = eratree[a]
factors.append(d)
a //= d
...
This page is auto-translated from /nishio/高速素因数分解 using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.