8000 Merge pull request #21 from mohbius/patch-1 · jeffmikels/python@66bfe1b · GitHub
[go: up one dir, main page]

Skip to content

Commit 66bfe1b

Browse files
authored
Merge pull request AllAlgorithms#21 from mohbius/patch-1
Create nth_fibonacci_using_goldenratio.py
2 parents bdc5906 + 10be5fc commit 66bfe1b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Python3 code to find n-th Fibonacci number
2+
# Adapted from code by Sharad Bhardwaj
3+
# Approximate value of golden ratio
4+
PHI = 1.6180339
5+
6+
# Fibonacci numbers upto n = 5
7+
f = [ 0, 1, 1, 2, 3, 5 ]
8+
9+
# Function to find nth
10+
# Fibonacci number
11+
def fib ( n ):
12+
13+
# Fibonacci numbers for n < 6
14+
if n < 6:
15+
return f[n]
16+
17+
# Else start counting from
18+
# 5th term
19+
t = 5
20+
fn = 5
21+
22+
while t < n:
23+
fn = round(fn * PHI)
24+
t+=1
25+
26+
return fn
27+
28+
#OUTPUTING 34
29+
print(n, "th Fibonacci Number =", fib(9))
30+
#OUTPUTING 21
31+
print(n, "th Fibonacci Number =", fib(8))
32+
#OUTPUTING 13
33+
print(n, "th Fibonacci Number =", fib(7))

0 commit comments

Comments
 (0)
0