mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
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:
parent
06ab650e08
commit
71ba3a1ad9
@ -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
|
What is the value of the first triangle number to have over five hundred
|
||||||
divisors?
|
divisors?
|
||||||
"""
|
"""
|
||||||
from math import sqrt
|
|
||||||
|
|
||||||
|
|
||||||
def count_divisors(n):
|
def count_divisors(n):
|
||||||
nDivisors = 0
|
nDivisors = 1
|
||||||
for i in range(1, int(sqrt(n)) + 1):
|
i = 2
|
||||||
if n % i == 0:
|
while i * i <= n:
|
||||||
nDivisors += 2
|
multiplicity = 0
|
||||||
# check if n is perfect square
|
while n % i == 0:
|
||||||
if n ** 0.5 == int(n ** 0.5):
|
n //= i
|
||||||
nDivisors -= 1
|
multiplicity += 1
|
||||||
|
nDivisors *= multiplicity + 1
|
||||||
|
i += 1
|
||||||
|
if n > 1:
|
||||||
|
nDivisors *= 2
|
||||||
return nDivisors
|
return nDivisors
|
||||||
|
|
||||||
|
|
||||||
@ -39,9 +42,8 @@ def solution():
|
|||||||
"""Returns the value of the first triangle number to have over five hundred
|
"""Returns the value of the first triangle number to have over five hundred
|
||||||
divisors.
|
divisors.
|
||||||
|
|
||||||
# The code below has been commented due to slow execution affecting Travis.
|
>>> solution()
|
||||||
# >>> solution()
|
76576500
|
||||||
# 76576500
|
|
||||||
"""
|
"""
|
||||||
tNum = 1
|
tNum = 1
|
||||||
i = 1
|
i = 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user