def indices_subsequencia_fibonacci(X):
if (X[0] < 0):
return -1
n = len(X)
fib1, fib2 = 0, 1
indice_fib = 1
while (X[0] >= fib1):
if (X[0] == fib1):
j = 0
indice_fib_inicial = indice_fib
while ((j < n) and (X[j]==fib1)):
fib1, fib2, indice_fib = fib2, fib1+fib2, indice_fib+1
j = j + 1
if (j == n):
return indice_fib_inicial, indice_fib-1
else:
return -1
fib1, fib2, indice_fib = fib2, fib1+fib2, indice_fib+1
return -1
if __name__ == '__main__':
X1 = [34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584]
print X1
print indices_subsequencia_fibonacci(X1)
X2 = [34, 56, 89, 144, 233, 377, 610, 987, 1597, 2584]
print X2
print indices_subsequencia_fibonacci(X2)
X3 = [34, 89, 144, 233, 377, 610, 987, 1597, 2584]
print X3
print indices_subsequencia_fibonacci(X3)
Algum erro? Favor me avisar. Dúvidas e sugestões são sempre bem-vindos!