import math
from math import *
def p12d(int1):
# based on factorization
# http://answers.yahoo.com/question/index?qid=20071208133532AAPW5Q7
# i and tri are for advancing the triangular number
i = 1
tri = 1
factors = 1
# loop until there are more than int1 factors
while factors <= int1:
factors = 1
##########
# Calculate the number of factors that tri has.
prime = 1
temp = tri
# STOP WHEN PRIME IS BIGGER THAN TRIANGLE:
while prime <= tri:
# GENERATE THE NEXT PRIME
prime = prime + 1
primestat = True
rangel = int(sqrt(prime)) + 1
for item in range(rangel, 1, -1):
if prime % item == 0:
primestat = False
break
# IF YOU GET A PRIME
if primestat == True or prime == 2:
# DIVIDE AS MANY TIMES AS POSSIBLE
t = 1
while temp % prime == 0:
temp = temp / prime
t += 1
factors *= t
##########
# keep together
i += 1
tri += i
# when done, what is the triangle number that meets requirements?
return tri - i