'''
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 math
from collections import Counter
def get_factor(a):
    if a==2:
        return 1
    for fact in range(2,math.ceil(math.sqrt(a))+1):
        if a % fact == 0:
            return  fact
            break
        elif fact== math.ceil(math.sqrt(a)):
            return 1
    
def get_re(a):
    n = math.ceil(math.log(a,2))+1
    dp = [[0]*n]*n
    dp[0][0] = a
    re = []
    for i in range(n):
        flag = 0
        for j in range(i+1):
            factor = get_factor(dp[i][j])
            if factor == 1:
                dp[i+1][j] = dp[i][j]
            else:
                a = dp[i][j]//factor
                dp[i+1][j] = factor
                dp[i+1][j+1] = a
                flag=1
        if flag==0:
            re = dp[i+1]
            break

    b = Counter(re)
    b.pop(0)
    print(b)
    product = 1
    for i in b.values():
        product *= (i+1)
    return product

i = 3
p = 1
while p < 500: 
    p_1 = get_re(i//(2-i%2))
    p_2 = get_re((i+1)//((i%2)+1))
    p = p_1*p_2
    i += 1
print(p_1,p_2,i)
print(i*(i-1)//2)

        
