Skip to content

Commit 765be45

Browse files
MaximSmolskiygithub-actions
and
github-actions
authored
Improve Project Euler problem 012 solution 2 (TheAlgorithms#5760)
* Improve solution * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 0ea5c73 commit 765be45

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

Diff for: project_euler/problem_012/sol2.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,18 @@ def triangle_number_generator():
2929

3030

3131
def count_divisors(n):
32-
return sum(2 for i in range(1, int(n ** 0.5) + 1) if n % i == 0 and i * i != n)
32+
divisors_count = 1
33+
i = 2
34+
while i * i <= n:
35+
multiplicity = 0
36+
while n % i == 0:
37+
n //= i
38+
multiplicity += 1
39+
divisors_count *= multiplicity + 1
40+
i += 1
41+
if n > 1:
42+
divisors_count *= 2
43+
return divisors_count
3344

3445

3546
def solution():

0 commit comments

Comments
 (0)