I need to speed up this code so it can handle inputs in the thousands. It adds triangular numbers to a list based on input n, then it loops through to find if any two consecutive numbers squared in that list add up to n. The test cases IveI've been using are n = 45, which would be true and n = 6, which would be false.
Here is the code:
def Triangular(n):
lst = []
for i in range(1, n + 1):
lst.append((i** 2 + i)//2)
for i in lst:
for j in lst:
if i*i + j*j == n and ((lst.index(i) == lst.index(j)+1)
or (lst.index(i) == lst.index(j)-1)):
return True
else:
continue