Moved more problems to Python.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
try:
|
||||
from lib_misc import get_item_counts
|
||||
from lib_misc import product
|
||||
except ModuleNotFoundError:
|
||||
from .lib_misc import get_item_counts
|
||||
from .lib_misc import product
|
||||
|
||||
|
||||
def prime_factors(n):
|
||||
@@ -107,3 +109,36 @@ def primes(n_max):
|
||||
if b[i - 1]:
|
||||
ps.append(i)
|
||||
return ps
|
||||
|
||||
|
||||
def get_divisors_count(n):
|
||||
"""
|
||||
Returns the number of divisors for n.
|
||||
The numbers 1 and n count as a divisor.
|
||||
|
||||
>>> get_divisors_count(1)
|
||||
1
|
||||
>>> get_divisors_count(3)
|
||||
2 # 1, 3
|
||||
>>> get_divisors_count(4)
|
||||
3 # 1, 2, 4
|
||||
|
||||
Getting the number of divisors is a combinatorial
|
||||
problem that can be solved by using the counts
|
||||
for each prime factor. For example, consider
|
||||
|
||||
2 * 2 * 7 = 28
|
||||
|
||||
We have 3 options for 2 (1, 1 * 2, 2 * 2)
|
||||
and 2 options for 7 (1, 1 * 7).
|
||||
|
||||
By multiplying those options we get the number
|
||||
of combinations:
|
||||
|
||||
2 * 3 = 6
|
||||
"""
|
||||
if n == 1:
|
||||
return 1
|
||||
factors = prime_factors_count(n)
|
||||
count = product([v + 1 for v in factors.values()])
|
||||
return count
|
||||
|
||||
Reference in New Issue
Block a user