Skip to content

Instantly share code, notes, and snippets.

@OussaZaki
Created June 28, 2017 16:50
Show Gist options
  • Save OussaZaki/39d30a1ea98fefc28024e4d339dda15d to your computer and use it in GitHub Desktop.
Save OussaZaki/39d30a1ea98fefc28024e4d339dda15d to your computer and use it in GitHub Desktop.
Explicit expression of the sum of the even Fibonacci numbers using the Python Decimal library
from math import sqrt, floor, log
from decimal import *
context = Context(prec=3000, rounding=ROUND_05UP)
setcontext(context)
phi = Decimal(1 + Decimal(5).sqrt()) / Decimal(2)
psi = Decimal(1 - Decimal(5).sqrt()) / Decimal(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 = context.power(phi, 3)
psi3 = context.power(psi, 3)
return int((Decimal(1) / Decimal(5).sqrt()) * (
phi3 * ((1 - context.power(phi3, k)) / (1 - phi3)) -
psi3 * ((1 - context.power(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