#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?
prastevila = [2,3,5,7,11,13,17] # tabela prastevil, ki so pogosti prafakotrji 

def stevDeliteljev(tabelaFaktorjev):
    ''' Vrne stevilo deliteljev glede na (pra)faktorje stevila '''
    prod = 1                                        # izkaze se, da prafaktorji stevila, zapisani v obliki p1^a,p2^b,p3^c,...
    for prastev in prastevila:                      # (px - prafaktor;a,b,c,... - stevilo ponovitev prafaktorja) pokazejo
        prod *= tabelaFaktorjev.count(prastev) + 1  # stevilo deliteljev, ki jih ima stevilo, ki smo ga razcepili na prafaktorje. 
    return prod                                     # In sicer po formuli (a + 1)*(b + 1)*(c + 1)*.... 

def fakotriziraj(stevilo):
    ''' REKURZIVNA Vrne tabelo, ki vsebuje (pra)faktorje danega stevila '''
    if stevilo in prastevila: # ali je stevilo prastevilo 
        return [stevilo]
    for prastev in prastevila: # ali se, da stevilo še enkrat razcepiti
        if stevilo % prastev == 0:
            return fakotriziraj(prastev) + fakotriziraj(stevilo//prastev) # razcepimo na prafaktor in (pra)faktor
    return [stevilo] # ce nic od zgoraj ne vjame stevila ima stevilo "cudne" faktorje, ki jih je malo
        
triangleNum = 1
naravnoStevilo = 1

while stevDeliteljev(fakotriziraj(triangleNum)) <= 500:
    naravnoStevilo += 1
    triangleNum += naravnoStevilo
print(triangleNum)