Skip to content

Commit 166adcf

Browse files
authored
Merge pull request #25 from mdoumbouya/master
pythagoras, trigonometry, prime factorization, golden ratio
2 parents d8a3aac + 7013ef5 commit 166adcf

29 files changed

Lines changed: 1040 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>

en/prime-factorization/index.html

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
2+
<title>Prime Factorization</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>Prime Factorization</h1>
38+
<p class="subtleHeading">Problem written by Moussa Doumbouya, discovered by Euclid</p>
39+
<hr/>
40+
41+
42+
43+
<p>
44+
<a href="https://en.wikipedia.org/wiki/Integer_factorization#Prime_decomposition">Prime factorization</a> is the process of
45+
decomposing an integer into factors that are prime numbers.
46+
<a href="https://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic">The fundamental theorem of arithmetic</a> states
47+
that every positive integer has a unique prime factorization. Here are examples of prime factorizations:
48+
49+
<ul>
50+
<li>14 = 2 x 7</li>
51+
<li>25 = 5 x 5</li>
52+
<li>99 = 3 x 3 x 11</li>
53+
<li>255 = 3 x 5 x 17</li>
54+
<li>1234 = 2 x 617</li>
55+
</ul>
56+
</p>
57+
58+
59+
<p>
60+
Write a program that continually reads in an integer and outputs its prime factors. Here is a possible
61+
top-down decomposition of the problem.
62+
<ul>
63+
<li>
64+
A function that shows the prime factors of a number by repeatedly finding its smallest prime factor
65+
and repeating the same process on the remaining quotient until the remaining quotient is 1.
66+
</li>
67+
<li>
68+
A function that returns the smallest prime factor of its argument.
69+
That is, the smallest number in the interval [2, n] that is a prime number and that evenly divides the argument.
70+
</li>
71+
<li>
72+
A function that checks if its argument is prime.
73+
Note that a prime numer is a number that is not evenly divisible by any number other than 1 and itself.
74+
</li>
75+
</ul>
76+
</p>
77+
78+
<p>
79+
Examples of sample inputs and outputs of your program:
80+
<pre>
81+
82+
Enter an integer: <span class="blue">34</span>
83+
The prime factors of 34 are:
84+
2
85+
17
86+
87+
Enter an integer: <span class="blue">28</span>
88+
The prime factors of 28 are:
89+
2
90+
2
91+
7
92+
93+
Enter an integer: <span class="blue">147</span>
94+
The prime factors of 147 are:
95+
3
96+
7
97+
7
98+
99+
</pre>
100+
</p>
101+
102+
<h2>Solution</h2>
103+
<pre class="console" id="editor" style="height:1580.0px">&quot;&quot;&quot;
104+
This program performs prime factorization onto user-specified positive integers.
105+
&quot;&quot;&quot;
106+
107+
def main():
108+
while True:
109+
n = read_positive_int(&quot;Enter an integer: &quot;)
110+
show_prime_factors(n)
111+
print(&quot;&quot;)
112+
113+
114+
def read_positive_int(prompt_msg):
115+
&quot;&quot;&quot;
116+
Reads a positive integer entered by the user.
117+
118+
Args:
119+
prompt_msg (str): The prompt message to show the user when requesting the input.
120+
121+
Returns:
122+
int: The positive integer entered by the user.
123+
&quot;&quot;&quot;
124+
n = int(input(prompt_msg))
125+
while n&lt;=0:
126+
print(&quot;Please enter a positive integer&quot;)
127+
n = int(input(prompt_msg))
128+
129+
return n
130+
131+
132+
def show_prime_factors(n):
133+
&quot;&quot;&quot;
134+
Show the prime factors of n
135+
136+
Args:
137+
n (int): The number to be factorized
138+
&quot;&quot;&quot;
139+
140+
print(&quot;The prime factors of &quot; + str(n) + &quot; are: &quot;)
141+
while n&gt;1:
142+
p = smallest_prime_factor(n)
143+
print(p)
144+
n = n // p
145+
146+
147+
def smallest_prime_factor(n):
148+
&quot;&quot;&quot;
149+
Compute the smallest prime factor of n.
150+
151+
Args:
152+
n (int): The number to be factorized
153+
154+
Returns:
155+
int: The smallest number in the range [2, n] that is
156+
a divisor of n, and is a prime number
157+
&quot;&quot;&quot;
158+
for i in range(2, n+1):
159+
if n%i == 0 and is_prime(i):
160+
return i
161+
162+
163+
def is_prime(n):
164+
&quot;&quot;&quot;
165+
Test if the specified number is prime.
166+
167+
Args:
168+
n (int): The number to be tested
169+
170+
Returns:
171+
bool: True if n has no divisor in range [2, n[. False otherwise.
172+
&quot;&quot;&quot;
173+
for d in range(2, n):
174+
if n%d == 0:
175+
return False
176+
return True
177+
178+
179+
180+
if __name__ == &#039;__main__&#039;:
181+
main()</pre>
182+
183+
<script src="../../plugins/ace/ace.js" type="text/javascript" charset="utf-8"></script>
184+
<script>
185+
var editor = ace.edit("editor");
186+
editor.setTheme('ace/theme/eclipse');
187+
editor.getSession().setMode("ace/mode/python");
188+
editor.setReadOnly(true);
189+
editor.renderer.setShowGutter(false);
190+
editor.setFontSize("14px");
191+
/*editor.setTheme("ace/theme/eclipse");
192+
editor.getSession().setMode("ace/mode/java");*/
193+
</script>
194+
<hr/>
195+
</div>
196+
</div>
197+
</div>
198+
199+
</body>

0 commit comments

Comments
 (0)