# https://projecteuler.net/problem=12

# 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?



triangle_numbers = [1]

def create_triangle_number(range_of_numbers):
	for i in xrange(1, range_of_numbers + 1):
		mid_num = 0
		for j in xrange(i, len(triangle_numbers) - 1, -1):
			mid_num += i
		triangle_numbers.append(mid_num)
		divisors = how_many_divisors(mid_num)
		print divisors
		if divisors > 500:
			print 'The first triangle number to have over 500 divisors is' + \
				  str(mid_num)
			return divisors


def how_many_divisors(triangle_number):
	divisors = [1, triangle_number]
	for i in xrange(2, triangle_number - 1):
		if triangle_number % i == 0:
			divisors.append(i)
			for j in xrange(2, len(divisors) -1):
				if divisors[j] * i < triangle_number:
					divisors.append(divisors[j] * i)
	return len(divisors)

create_triangle_number(1000000)