forked from OmkarPathak/pygorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoldenratio.py
More file actions
43 lines (32 loc) · 928 Bytes
/
goldenratio.py
File metadata and controls
43 lines (32 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
Fibonacci implementation through golden ratio (math formula).
"""
import inspect
import math
def get_sequence(n):
"""
Return Fibonacci sequence from zero
to specified number as list.
"""
return sequence(n)
def fib(n):
"""
Return Fibonacci value by specified number as integer.
Golden ratio —
https://en.wikipedia.org/wiki/Golden_ratio
Fibonacci's relation to the golden ratio —
https://en.wikipedia.org/wiki/Fibonacci_number#Closed-form_expression
"""
golden_ratio = (1 + math.sqrt(5)) / 2
val = (golden_ratio ** n - (1 - golden_ratio) ** n) / math.sqrt(5)
return int(val)
def sequence(n):
"""
Return sequence of Fibonacci values as list.
"""
return [fib(value) for value in range(n + 1)]
def get_code():
"""
Return source code of Fibonacci sequence logic's implementation.
"""
return inspect.getsource(get_sequence)