Skip to content

Commit 7013ef5

Browse files
committed
golden-ratio
1 parent 8edce15 commit 7013ef5

6 files changed

Lines changed: 321 additions & 0 deletions

File tree

en/golden-ratio/index.html

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
2+
<title>Golden Ratio</title>
3+
4+
<meta name="viewport" content="width=device-width, initial-scale=1">
5+
6+
<meta http-equiv="content-type" content="text/html; charset=UTF8">
7+
8+
9+
<!-- Bootstrap -->
10+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
11+
<link rel="stylesheet" type="text/css" href="../../style.css">
12+
13+
<!-- Java Script -->
14+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
15+
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
16+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
17+
18+
19+
20+
<!-- font awesome -->
21+
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
22+
23+
<!-- SWAL -->
24+
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
25+
26+
27+
<!-- Stanford -->
28+
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700' rel='stylesheet' type='text/css'>
29+
<link href='https://fonts.googleapis.com/css?family=Source+Serif+Pro:400,600,700' rel='stylesheet' type='text/css'>
30+
31+
32+
<body>
33+
34+
<div class="container container-course">
35+
<div class="row">
36+
<div class="col">
37+
<h1>Golden Ratio</h1>
38+
<p class="subtleHeading">Problem written by Moussa Doumbouya</p>
39+
<hr/>
40+
<p>
41+
When two numbers a and b are such that their ratio (a/b) is the same as the ratio
42+
of their sum to the largest of the two numbers (a+b)/a, they are said to be in the <a href="https://en.wikipedia.org/wiki/Golden_ratio">golden ratio.</a>
43+
<br /><br />
44+
The golden ratio is then: ϕ&nbsp;&nbsp;=&nbsp;&nbsp;(a+b) / a&nbsp;&nbsp;=&nbsp;&nbsp;a / b.
45+
<br /><br />
46+
The Fibonacci sequence is the sequence is such that the first two elements are 1,
47+
and any other element is the sum of the two elements preceeding it.
48+
<ul>
49+
<li>F(0) = 1</li>
50+
<li>F(1) = 1</li>
51+
<li>F(2) = F(1) + F(0) = 2</li>
52+
<li>F(3) = F(2) + F(1) = 3</li>
53+
<li>F(4) = F(3) + F(2) = 5</li>
54+
<li>F(5) = F(4) + F(3) = 8</li>
55+
<li>F(6) = F(5) + F(4) = 13</li>
56+
<li>F(7) = F(6) + F(5) = 21</li>
57+
<li>...</li>
58+
<li>F(n) = F(n-1) + F(n-2)</li>
59+
60+
</ul>
61+
62+
The ratio of consecutive Fibonaccy terms 1/1, 2/1, 3/2, 5/3, 8/5, 13/8, 21/13, etc. for larger and larger terms converges towards
63+
one number, ... the golden ratio!
64+
</p>
65+
66+
<p>
67+
Write a program that estimates the golden ratio as the ratio of consecutive Fibonaccy terms.
68+
Here is a possible top-down decomposition.
69+
70+
<ul>
71+
<li>A function that displays the golden ratio at position i (in Fibonacci sequence) for i going from 1 to 100</li>
72+
<li>A function that computes the golden ratio at position i, F(i) / F(i-1)</li>
73+
<li>A function that computes the Fibonaccy term i, F(i)</li>
74+
</ul>
75+
</p>
76+
77+
<p>
78+
Here is a sample of the expected output of your program:
79+
</p>
80+
81+
82+
<pre class="console" style="width:400px">
83+
1.0
84+
2.0
85+
1.5
86+
1.6666666666666667
87+
1.6
88+
1.625
89+
1.6153846153846154
90+
1.619047619047619
91+
1.6176470588235294
92+
1.6181818181818182
93+
1.6179775280898876
94+
1.6180555555555556
95+
1.6180257510729614
96+
1.6180371352785146
97+
1.618032786885246
98+
1.618034447821682
99+
1.6180338134001253
100+
1.618034055727554
101+
1.6180339631667064
102+
1.6180339985218033
103+
1.618033985017358
104+
1.6180339901755971
105+
1.618033988205325
106+
1.618033988957902
107+
1.6180339886704431
108+
1.6180339887802426
109+
1.618033988738303
110+
1.6180339887543225
111+
1.6180339887482036
112+
1.6180339887505408
113+
1.6180339887496482
114+
1.618033988749989
115+
1.618033988749859
116+
1.6180339887499087
117+
1.6180339887498896
118+
1.618033988749897
119+
1.618033988749894
120+
1.6180339887498951
121+
1.6180339887498947
122+
1.618033988749895
123+
1.618033988749895
124+
1.618033988749895
125+
1.618033988749895
126+
1.618033988749895
127+
1.618033988749895
128+
1.618033988749895
129+
...
130+
</pre>
131+
132+
133+
134+
135+
<h2>Solution</h2>
136+
<pre class="console" id="editor" style="height:660.0px">
137+
def main():
138+
&quot;&quot;&quot;
139+
Show the ratio of consecutive Fibonaccy numbers
140+
&quot;&quot;&quot;
141+
for i in range(1, 100):
142+
print(golden(i))
143+
144+
145+
def golden(n):
146+
&quot;&quot;&quot;
147+
Return the ratio of fibonacci number (n) to fibonacci number (n-1)
148+
&quot;&quot;&quot;
149+
return fib(n)/fib(n-1)
150+
151+
152+
def fib(n):
153+
&quot;&quot;&quot;
154+
Compute fibonacci term n
155+
&quot;&quot;&quot;
156+
a = 1
157+
b = 1
158+
159+
for i in range(1, n+1):
160+
next_element = a + b
161+
a = b
162+
b = next_element
163+
164+
return a
165+
166+
167+
if __name__ == &#039;__main__&#039;:
168+
main()</pre>
169+
170+
<script src="../../plugins/ace/ace.js" type="text/javascript" charset="utf-8"></script>
171+
<script>
172+
var editor = ace.edit("editor");
173+
editor.setTheme('ace/theme/eclipse');
174+
editor.getSession().setMode("ace/mode/python");
175+
editor.setReadOnly(true);
176+
editor.renderer.setShowGutter(false);
177+
editor.setFontSize("14px");
178+
/*editor.setTheme("ace/theme/eclipse");
179+
editor.getSession().setMode("ace/mode/java");*/
180+
</script>
181+
<hr/>
182+
</div>
183+
</div>
184+
</div>
185+
186+
</body>

examples/golden-ratio/credit.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Problem written by Moussa Doumbouya

examples/golden-ratio/index.html

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<p>
2+
When two numbers a and b are such that their ratio (a/b) is the same as the ratio
3+
of their sum to the largest of the two numbers (a+b)/a, they are said to be in the <a href="https://en.wikipedia.org/wiki/Golden_ratio">golden ratio.</a>
4+
<br /><br />
5+
The golden ratio is then: ϕ&nbsp;&nbsp;=&nbsp;&nbsp;(a+b) / a&nbsp;&nbsp;=&nbsp;&nbsp;a / b.
6+
<br /><br />
7+
The Fibonacci sequence is the sequence is such that the first two elements are 1,
8+
and any other element is the sum of the two elements preceeding it.
9+
<ul>
10+
<li>F(0) = 1</li>
11+
<li>F(1) = 1</li>
12+
<li>F(2) = F(1) + F(0) = 2</li>
13+
<li>F(3) = F(2) + F(1) = 3</li>
14+
<li>F(4) = F(3) + F(2) = 5</li>
15+
<li>F(5) = F(4) + F(3) = 8</li>
16+
<li>F(6) = F(5) + F(4) = 13</li>
17+
<li>F(7) = F(6) + F(5) = 21</li>
18+
<li>...</li>
19+
<li>F(n) = F(n-1) + F(n-2)</li>
20+
21+
</ul>
22+
23+
The ratio of consecutive Fibonaccy terms 1/1, 2/1, 3/2, 5/3, 8/5, 13/8, 21/13, etc. for larger and larger terms converges towards
24+
one number, ... the golden ratio!
25+
</p>
26+
27+
<p>
28+
Write a program that estimates the golden ratio as the ratio of consecutive Fibonaccy terms.
29+
Here is a possible top-down decomposition.
30+
31+
<ul>
32+
<li>A function that displays the golden ratio at position i (in Fibonacci sequence) for i going from 1 to 100</li>
33+
<li>A function that computes the golden ratio at position i, F(i) / F(i-1)</li>
34+
<li>A function that computes the Fibonaccy term i, F(i)</li>
35+
</ul>
36+
</p>
37+
38+
<p>
39+
Here is a sample of the expected output of your program:
40+
</p>
41+
42+
43+
<pre class="console" style="width:400px">
44+
1.0
45+
2.0
46+
1.5
47+
1.6666666666666667
48+
1.6
49+
1.625
50+
1.6153846153846154
51+
1.619047619047619
52+
1.6176470588235294
53+
1.6181818181818182
54+
1.6179775280898876
55+
1.6180555555555556
56+
1.6180257510729614
57+
1.6180371352785146
58+
1.618032786885246
59+
1.618034447821682
60+
1.6180338134001253
61+
1.618034055727554
62+
1.6180339631667064
63+
1.6180339985218033
64+
1.618033985017358
65+
1.6180339901755971
66+
1.618033988205325
67+
1.618033988957902
68+
1.6180339886704431
69+
1.6180339887802426
70+
1.618033988738303
71+
1.6180339887543225
72+
1.6180339887482036
73+
1.6180339887505408
74+
1.6180339887496482
75+
1.618033988749989
76+
1.618033988749859
77+
1.6180339887499087
78+
1.6180339887498896
79+
1.618033988749897
80+
1.618033988749894
81+
1.6180339887498951
82+
1.6180339887498947
83+
1.618033988749895
84+
1.618033988749895
85+
1.618033988749895
86+
1.618033988749895
87+
1.618033988749895
88+
1.618033988749895
89+
1.618033988749895
90+
...
91+
</pre>
92+
93+
94+

examples/golden-ratio/soln.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
def main():
3+
"""
4+
Show the ratio of consecutive Fibonaccy numbers
5+
"""
6+
for i in range(1, 100):
7+
print(golden(i))
8+
9+
10+
def golden(n):
11+
"""
12+
Return the ratio of fibonacci number (n) to fibonacci number (n-1)
13+
"""
14+
return fib(n)/fib(n-1)
15+
16+
17+
def fib(n):
18+
"""
19+
Compute fibonacci term n
20+
"""
21+
a = 1
22+
b = 1
23+
24+
for i in range(1, n+1):
25+
next_element = a + b
26+
a = b
27+
b = next_element
28+
29+
return a
30+
31+
32+
if __name__ == '__main__':
33+
main()

examples/golden-ratio/tags.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
week3
2+
expressions
3+
functions
4+
parameters
5+
returns
6+
decomposition

examples/golden-ratio/title.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Golden Ratio

0 commit comments

Comments
 (0)