'''
Problem 012

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 500 divisors? 


Solution: Copyright 2017 Dave Cuthbert, MIT License
'''

def generate_triangle_number():
    current = 0
    count = 1
    while True:
        current += count
        count += 1
        yield current


def solve_problem():
    triangle_number = generate_triangle_number()
    for i in range(10):
       print(next(triangle_number))

    return "DONE"


if __name__ == "__main__":
    print(solve_problem())

'''
Problem 012

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 500 divisors? 


Solution: Copyright 2017 Dave Cuthbert, MIT License
'''
import math


def generate_triangle_number():
    current = 0
    count = 1
    while True:
        current += count
        count += 1
        yield current


def find_factors(number):
    last_divisor = int(math.sqrt(number)) + 1
    factors = set([])
    found = -1
    for divisor in range(1, last_divisor):
        if (number % divisor == 0):
            factors.add(divisor)
            factors.add(int(number / divisor))
    
    if len(factors) > 500:
        found = number

    return found


def solve_problem():
    triangle_number = generate_triangle_number()

    while True:
        number = find_factors(next(triangle_number))
        if number > 0:
           return number 


if __name__ == "__main__":
    print(solve_problem())

Solve 012