I wrote a program about triangular numbers, which are derived from the sum of natural numbers one after the other. For example, the third triangular number is equal to: 1 + 2 + 3 = 6.
I want to find divisors of triangular numbers. for example the third triangular number have 4 divisors which equal to : 1,2,3,6.
Some triangular number with their divisors:
- 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
I want to find the smallest triangular number which has more than 500 divisors. This program works, but is too slow to find the 500th. How can I optimize my program?
def triangular(n):
a= []
for i in range (2,n):
if n % i == 0 :
a.append(i)
if len(a) > 498:
return True
else :
a.clear()
return False
x=1
n=x
while x>0:
if triangular(n):
print(n)
break
else:
n+=x+1
x+=1