Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. # 28 is the first triangle number to have over five divisors.
  2. # What is the value of the first triangle number to have over five hundred divisors?
  3.  
  4. from math import *
  5.  
  6. # Function to calculate divisors
  7. def divisors(n):
  8. limit = int(sqrt(n))
  9. divisors_list = []
  10. for i in range(1, limit+1, 1):
  11. if n % i == 0:
  12. divisors_list.append(i)
  13. if i != n/i:
  14. divisors_list.append(n/i)
  15. return len(divisors_list)
  16.  
  17. # Function to check for triangle number
  18.  
  19. def isTriangleNumber(n):
  20. a = int(sqrt(2*n))
  21. return 0.5*a*(a+1) == n
  22. # Function to calculate the last term of the series adding up to the triangle number
  23. def lastTerm(n):
  24. if isTriangleNumber(n):
  25. return int(sqrt(2*n))
  26. else:
  27. return None
  28.  
  29. # First number 'check' to have 500 divisors
  30. check = 2**4 * 3**4 * 5**4 * 7 * 11
  31.  
  32. # Starting from 'check', iterate sequentially checking for the next 'triangle' number
  33. while not isTriangleNumber(check):
  34. check += 1
  35. # Calculate the last term of the series ('seriesLastTerm') that adds up to the newly calculated triangle number 'check'
  36. seriesLastTerm = lastTerm(check)
  37.  
  38. # Iterate over triangle numbers checking for divisors > 500
  39. while divisors(check) <= 500:
  40. # add the next term to check to get the next triangle number
  41. check += (seriesLastTerm + 1)
  42. seriesLastTerm += 1
  43. print check
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement