raspeball

Project Euler #12

Apr 21st, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def trianglenum(n): #computes the n-th triangle number
  4.     trinum = (n*(n+1))/2
  5.     return trinum
  6.  
  7. def divnum(x):
  8.     d = 0
  9.     largeds = []
  10.     for i in xrange(1,int((np.sqrt(x) + 1))):
  11.             if x % i == 0:
  12.                 d = d + 1
  13.                 if i*i != x:
  14.                     largeds.append((x / i))
  15.             else: continue
  16.     totd = d + len(largeds)
  17.     return totd
  18.  
  19.  
  20. def main():
  21.     tnums = 1 # #-th triangular number
  22.     while tnums >= 1:
  23.         triangle = trianglenum(tnums)
  24.         trianglediv = divnum(triangle)
  25.         if trianglediv >= 500:
  26.             break
  27.         else:
  28.             tnums = tnums + 1
  29.         #
  30.     return (triangle, tnums)
  31.  
  32. print(main())
Advertisement
Add Comment
Please, Sign In to add comment