Improve Project Euler problem 012 solution 1 (#5731)

* Improve solution

* Uncomment code that has been commented due to slow execution affecting Travis

* Retest
This commit is contained in:
Maxim Smolskiy
2021-11-01 09:27:19 +03:00
committed by GitHub
parent 06ab650e08
commit 71ba3a1ad9

View File

@ -21,17 +21,20 @@ We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred
divisors?
"""
from math import sqrt
def count_divisors(n):
nDivisors = 0
for i in range(1, int(sqrt(n)) + 1):
if n % i == 0:
nDivisors += 2
# check if n is perfect square
if n ** 0.5 == int(n ** 0.5):
nDivisors -= 1
nDivisors = 1
i = 2
while i * i <= n:
multiplicity = 0
while n % i == 0:
n //= i
multiplicity += 1
nDivisors *= multiplicity + 1
i += 1
if n > 1:
nDivisors *= 2
return nDivisors
@ -39,9 +42,8 @@ def solution():
"""Returns the value of the first triangle number to have over five hundred
divisors.
# The code below has been commented due to slow execution affecting Travis.
# >>> solution()
# 76576500
>>> solution()
76576500
"""
tNum = 1
i = 1