'''

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.

So we are supposed to find the value of the first triangle number to have over five
hundred divisors. 
For this we, create a class TriangularNumber with a method triang_num_calc. In this 
method, we create a list that stores a list of divisor of triangle numbers. Then we 
count the length of that list and if the length is greater than or equal to 500, we
add it to the dictionary, with key -> the number and value -> divisor's list. But if
the condition is not satisfied, we keep generating the triangle number and carry out
the process again and again.

'''



class TriangularNumber:
    
    def __init__(self):
        self.triang_div_lst = [] #list of divisors
        self.fact_dct = {} #dictionary for storing the key with the longest divisor
        self.num = 1 #initially set to 1

    def triang_num_calc(self):
        incrementor = 2 #since the our second element in triangle number is incremented 
                      # by 2,  we set it equal to 2
        while True:
            #generates a list of divisiors of a number
            self.triang_div_lst = [v for v in range(1, self.num+1) if self.num%v==0 ] #checking if get remainder 0 or not
            if len(self.triang_div_lst) >= 500: #if the length of divisor list is >= 500
                self.fact_dct[self.num] = self.triang_div_lst #adding that particular number to the dictionary  
                break                                         #with list of divisor as its value and breaking the loop
            else:
                self.num += incrementor #generating another triangle number
                incrementor += 1 #setting value for generating next triangle number.
        print(self.fact_dct)

TN = TriangularNumber()
TN.triang_num_calc()
            