Created
November 24, 2019 19:03
-
-
Save AmosAidoo/f82ddcbcea28750bbacc7ba4855df0be to your computer and use it in GitHub Desktop.
Highly Divisible Triangle Number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
#Variables to store n and our final answer | |
n, ans = 1, -1 | |
#Variable to store the highest number of divisors so far | |
div = 0 | |
while True: | |
#Compute the n-th triangle number | |
triangle_number = (n*(n+1)) // 2 | |
#Variable to store the number of divisors for the nth triangle number | |
divisors = 0 | |
#Compute the divisors and count them | |
for i in range(1, math.floor(math.sqrt(triangle_number)) + 1): | |
if triangle_number % i == 0: | |
if i != (triangle_number / i): | |
divisors += 2 | |
else: | |
divisors += 1 | |
#Store the maximum divisor | |
div = max(divisors, div) | |
#Break out of the loop if the divisor exceeds 500 | |
if div > 500: | |
ans = triangle_number | |
break | |
n += 1 | |
print("maximum divisor = ", ans) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment