import math
maxdivisor = 500
n = 1
while True:
#print(list(getfactors(triangle)))
sn = (n**2 + n) / 2
counter = 0
i = int(math.sqrt(sn))
while i >= 1:
if sn % i == 0:
counter += 1
i -= 1
lenfactors = counter + 1
#print(triangle, lenfactors)
if lenfactors > maxdivisor:
print(sn, lenfactors)
sys.exit(0)
n += 1
1. is the problem divisible into two or more parts? if not, goto 3
2. divide it, and for each part, execute step 3
3. would a lookup table be faster than actually running the code? if not, goto 6
4. make into lookup table
5. goto 1
6. your program is optimized
import math
maxdivisor = 501
n = 1
while True:
#print(list(getfactors(triangle)))
sn = sn + n ##(n**2 + n) / 2
counter = 0
maxi = int(math.sqrt(sn))
i=1
while (i <= maxi) + (counter<maxdivisor) == 2:
if sn % i == 0:
counter += 2
i += 1
##lenfactors = counter + 1
#print(triangle, lenfactors)
if lenfactors >= maxdivisor:
print(sn, lenfactors)
sys.exit(0)
n += 1