What could be the simplest and time efficient logic to find out the factors of a given Number.
Is there any algorithm that exist, based on the same.
Actually, my real problem is to find out the no. of factors that exist for a given Number..
So Any algorithm, please let me know on this..
Thanks.
Well, this is different. Let
nbe the given number.If
n = p1^e1 * p2^e2 * ... * pk^ek, where eachpis a prime number, then the number of factors ofnis(e1 + 1)*(e2 + 1)* ... *(ek + 1). More on this here.Therefore, it is enough to find the powers at which each prime factor appears. For example:
For example, take
18.18 = 2^1 * 3*2 => number of factors = (1 + 1)*(2 + 1) = 6. Indeed, the6factors of18are1, 2, 3, 6, 9, 18.Here’s a little benchmark between my method and the method described and posted by @Maciej. His has the advantage of being easier to implement, while mine has the advantage of being faster if change to only iterate over the prime numbers, as I have done for this test:
Results on my machine: