Skip to content

Improve Project Euler problem 012 solution 1 #5731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions project_euler/problem_012/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,29 @@
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


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
Expand Down