Created
June 28, 2017 15:41
-
-
Save OussaZaki/61cc7d2d2b4f4377a26a3f35eb12d65b to your computer and use it in GitHub Desktop.
Explicit expression of the sum of the even Fibonacci numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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