factorization
Efficiently getting all divisors of a given number
Factors are paired. 1 and 24, 2 and 12, 3 and 8, 4 and 6. An improvement of your algorithm could be to iterate to the square root of num instead of all the way to num, and then calculate the paired factors using num / i.
What is the fastest integer factorization algorithm?
Pulled directly from my answer to this other question. The method will work, but will be slow. “How big are your numbers?” determines the method to use: Less than 2^16 or so: Lookup table. Less than 2^70 or so: Richard Brent’s modification of Pollard’s rho algorithm. Less than 10^50: Lenstra elliptic curve factorization Less than … Read more
What is the most efficient way of finding all the factors of a number in Python?
from functools import reduce def factors(n): return set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))) This will return all of the factors, very quickly, of a number n. Why square root as the upper limit? sqrt(x) * sqrt(x) = x. So if the two factors are the … Read more