Skip to content

Instantly share code, notes, and snippets.

@OussaZaki
Created June 28, 2017 15:41
Show Gist options
  • Save OussaZaki/61cc7d2d2b4f4377a26a3f35eb12d65b to your computer and use it in GitHub Desktop.
Save OussaZaki/61cc7d2d2b4f4377a26a3f35eb12d65b to your computer and use it in GitHub Desktop.
Explicit expression of the sum of the even Fibonacci numbers
from math import sqrt, floor, log
phi = (1 + sqrt(5)) / 2
psi = (1 - sqrt(5)) / 2
def reverse_fib(fn):
return floor(log((fn * sqrt(5) + sqrt(5 * (fn ** 2) - 4)) / 2, phi))
def get_k(n):
return reverse_fib(n) // 3
def sum_even(k):
phi3 = phi ** 3
psi3 = psi ** 3
return int((1 / sqrt(5)) * (
phi3 * ((1 - phi3 ** k) / (1 - phi3)) -
psi3 * ((1 - psi3 ** k) / (1 - psi3))
))
t = int(input().strip())
for i in range(t):
N = int(input().strip())
k = get_k(N)
print(sum_even(k))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment