#!/usr/bin/python
# Copyright 2010 P. Wilczynski
# Project Euler
# Problem Number 14

import sys
import re

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

class Triangles:
    """A simple class to find the factors of a number that is triangular"""
    def __init__(self):
         self.t = 1
         self.n = 1
         self.dict_data = {}
         self.count = 0
         self.max_count = 0
    def iteration(self):
        self.n += 1
        self.t += self.n
        #print self.n, self.t
    def add_to_dict(self, k):
        list_k = [1,k]
        itera = range(2, k)
        itera.reverse()
        for i in itera:
            if (k%i == 0):
                if i in self.dict_data:
                    for m in self.dict_data[i]:
                        if m not in list_k:
                            list_k.append(m)
                else:
                    list_k.append(i)
                for l in list_k:
                    if l in itera:
                        itera.remove(l)
        self.dict_data[k] = list_k
        count = len(list_k)
        #print count, "count"
        return count
            
                        
                
                    
                    
                
            
            
        



"""

def num_div(n, dict_div):
    count = 0
    for i in range(1, n+1):
#        print i, n%i
        if n%i == 0:
            count+=1
    
    return count 
    
def iteration(triangle, n):
    n+=1
    triangle = triangle + n
    print triangle, n
    return triangle, n
    
"""
def triangle_nums(max):
  print "*****Triangle Numbers*****"
  print "methodology is a tree-style architecture with a dictionary"
  P12 = Triangles()
  count = 0
  large_count = 0
  while large_count<max:
      count = P12.add_to_dict(P12.t)
      if count > large_count: 
          large_count = count
          #print P12.dict_data[P12.t] 
      print P12.t, count
      print large_count
      P12.iteration()
      #print P12.dict_data


def main():
  number = long(sys.argv[1])
  triangle_nums(number)


if __name__ == '__main__':
  main()
