#The sequence of triangle numbers is generated by adding the natural numbers. 
#So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. 
#The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

#Let us list the factors of the first seven triangle numbers:
 #1: 1
 #3: 1,3
 #6: 1,2,3,6
 #10: 1,2,5,10
 #15: 1,3,5,15
 #21: 1,3,7,21
 #28: 1,2,4,7,14,28
#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?


import time
import math


def num_divisors(num):
	lim = int(math.sqrt(num))
	div_list = []
	for i in range(1, lim + 1):
		if (num % i == 0):
			div_list.append(i)
			if i != num/i:
				div_list.append(num/i)
	return len(div_list)

def isTriangleNum(num):
	a = int(math.sqrt(2*num))
	return (0.5*a*(a+1)) == num

def lastTerm(num):
	if isTriangleNum(num):
		return int(math.sqrt(2*num))
	else:
		return None

def main():
	start_time = time.time()

	check = 2**4 * 3**4 * 5**4 * 7 * 11
	
	while not isTriangleNum(check):
		check+=1

	seriesLastTerm = lastTerm(check)

	while num_divisors(check) <=500:
		check += (seriesLastTerm + 1)
		seriesLastTerm += 1

	print(check)
	

	elapsed_time = time.time() - start_time
	print("\nTime elapsed: " + str(elapsed_time))

main()