I learned an algorithm called "linear sieve" https://cp-algorithms.com/algebra/prime-sieve-linear.html that is able to get all primes numbers smaller than N
in linear time.
This algorithm has one by-product since it has an array lp[x]
that stores the minimal prime factor of the number x
.
So we can follow lp[x]
to find the first prime factor, and continue the division to get all factors.
In the meantime, the article also mentioned that with the help of one extra array, we can get all factors without division, how to achieve that?
The article said: "... Moreover, using just one extra array will allow us to avoid divisions when looking for factorization."