1. import math
  2. from math import *
  3.  
  4. def p12d(int1):
  5. # based on factorization
  6. # http://answers.yahoo.com/question/index?qid=20071208133532AAPW5Q7
  7.  
  8. # i and tri are for advancing the triangular number
  9. i = 1
  10. tri = 1
  11. factors = 1
  12.  
  13. # loop until there are more than int1 factors
  14. while factors <= int1:
  15. factors = 1
  16.  
  17. ##########
  18. # Calculate the number of factors that tri has.
  19.  
  20. prime = 1
  21. temp = tri
  22.  
  23. # STOP WHEN PRIME IS BIGGER THAN TRIANGLE:
  24. while prime <= tri:
  25.  
  26. # GENERATE THE NEXT PRIME
  27. prime = prime + 1
  28. primestat = True
  29. rangel = int(sqrt(prime)) + 1
  30. for item in range(rangel, 1, -1):
  31. if prime % item == 0:
  32. primestat = False
  33. break
  34.  
  35. # IF YOU GET A PRIME
  36. if primestat == True or prime == 2:
  37.  
  38. # DIVIDE AS MANY TIMES AS POSSIBLE
  39. t = 1
  40. while temp % prime == 0:
  41. temp = temp / prime
  42. t += 1
  43.  
  44. factors *= t
  45. ##########
  46.  
  47. # keep together
  48. i += 1
  49. tri += i
  50.  
  51. # when done, what is the triangle number that meets requirements?
  52. return tri - i