Improve Project Euler problem 012 solution 2 (#5760)

* Improve solution

* updating DIRECTORY.md

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Maxim Smolskiy 2021-11-03 23:32:10 +03:00 committed by GitHub
parent 0ea5c734e1
commit 765be4581e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,7 +29,18 @@ def triangle_number_generator():
def count_divisors(n): def count_divisors(n):
return sum(2 for i in range(1, int(n ** 0.5) + 1) if n % i == 0 and i * i != n) divisors_count = 1
i = 2
while i * i <= n:
multiplicity = 0
while n % i == 0:
n //= i
multiplicity += 1
divisors_count *= multiplicity + 1
i += 1
if n > 1:
divisors_count *= 2
return divisors_count
def solution(): def solution():