From fa2c6ce71208e287954e4157b30094fc6068a2f2 Mon Sep 17 00:00:00 2001 From: tdserapio <64088510+tdserapio@users.noreply.github.com> Date: Sat, 3 Oct 2020 14:42:05 +0800 Subject: [PATCH] Added Fibonnaci --- fib.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 fib.py diff --git a/fib.py b/fib.py new file mode 100644 index 0000000..5f78b0e --- /dev/null +++ b/fib.py @@ -0,0 +1,15 @@ +# This is n-th fibonnaci number, and only works if n >= 0 +def fib(n): + prev, curr = 1, 0 + for i in range(n): + prev, curr = curr, prev+curr + return curr + +# This is nearest fibonnaci number to n +def nearestFib(n): + prev, curr = 1, 0 + while curr <= n: + prev, curr = curr, prev+curr + if abs(prev+curr) < abs(curr): + return prev+curr + return curr