Skip to content
Advertisement

How to solve this equation with a loop?

This is the sequence:

1, 2, 3, -1, 7, -10, 3, -73, …

and actually it’s like this:

t(n) = (t(n-3) * t(n-2)) - t(n-1)

for example: -10 = (3 * -1) - 7.

I used this code but it’s not acting like this equation that I have provided.

n1 = 1
n2 = 2
n3 = 3
m = eval(input("number: "))
if m < 4:
    if m == n1:
        print(n1)
    elif m == n2:
        print(n2)
    elif m == n3:
        print(n3)
elif m >= 4:
    n4 = (n1 * n2 - n3)
    print(n4)

Advertisement

Answer

This is my solution to finding t(m)

n = [1,2,3]
m = int(input("number: ")) #eval is a big security risk!!!
while len(n) < m:
    n.append((n[-3] * n[-2] - n[-1]))
print(n[m-1])

where n[negative number] is a number from the end of the list.

Results in:

1, 2, 3, -1, 7, -10, 3, -73, 43, -262, -2877, -8389, 762163, 23372990, etc.
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement