Note that this question contains some spoilers.
A solution for problem #12 states that
“Number of divisors (including 1 and the number itself) can be calculated taking one element from prime (and power) divisors.”
The (python) code that it has doing this is num_factors = lambda x: mul((exp+1) for (base, exp) in factorize(x)) (where mul() is reduce(operator.mul, ...).)
It doesn’t state how factorize is defined, and I’m having trouble understanding how it works. How does it tell you the number of factors of the number?
The basic idea is that if you have a number factorized into the following form which is the standard form actually:
Now, to know how many divisors N has we have to take into consideration every combination of prime factors. So you could possibly say that the number of divisors is:
But you have to notice that if the exponent in the standard form of one of the prime factors is zero, then the result would also be a divisor. This means, we have to add one to each exponent to make sure we included the ith p to the power of zero. Here is an example using the number 12 — same as the question number 😀
I hope you see now why we add one to the exponent when calculating the divisors.