Skip to content

Instantly share code, notes, and snippets.

@AmosAidoo
Created November 24, 2019 19:03
Show Gist options
  • Save AmosAidoo/f82ddcbcea28750bbacc7ba4855df0be to your computer and use it in GitHub Desktop.
Save AmosAidoo/f82ddcbcea28750bbacc7ba4855df0be to your computer and use it in GitHub Desktop.
Highly Divisible Triangle Number
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