-
-
Notifications
You must be signed in to change notification settings - Fork 285
/
Basic mathematics.jl
419 lines (325 loc) Β· 14.7 KB
/
Basic mathematics.jl
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
### A Pluto.jl notebook ###
# v0.19.9
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : iv(el)
el
end
end
# βββ‘ 14158eb0-d45c-11ea-088f-330e45412320
a = 2
# βββ‘ 2ed4bb92-d45c-11ea-0b31-2d8e32ce7b44
b = 6
# βββ‘ 03664f5c-d45c-11ea-21b6-91cd647a07aa
md"# Mathematics in Julia π
This is an introduction to programming. Let's get started!
## Let's make a calculator!
First let's do some simple math with setting **a = $a**, **b = $b** and **c = a * b**. What will **c** equal?
Type in the cells (with the coloured background) below and press **`Shift-Enter`** or the click the right-arrow button (βΆοΈ) to the right to execute the cell after changing the values."
# βββ‘ e5e0a0da-d45c-11ea-1042-e9b5d0654d4f
md"Fix the value of `c` below to make it `c = a * b`"
# βββ‘ 30f0f882-d45c-11ea-2adc-7d84ecf8a7a6
c = 10
# βββ‘ 262b312a-d460-11ea-26c5-df30459effc5
people = 10
# βββ‘ 2ea7f162-d460-11ea-0e8e-25340e2e64da
avg = 2.5
# βββ‘ 3da812c6-d460-11ea-0170-79fbb6a4347c
slices = 8
# βββ‘ a38cb92e-d45e-11ea-2959-05be909befb2
md"""### Now you have a calculator!
You did multiplication above. Here's how you do other mathematical operations:
Operation | Type This
:------------ | :-------------:
add | +
subtract | -
multiply | *
divide | /
power | ^
### Pizza Slices
Let's try this out on a problem. Let's say you want to order pizzas for $people people (**people = $people**) and each person wants $avg slices on average (**avg = $avg**). A pizza has $slices slices per pizza (**slices = $slices**). How many pizzas should you order (**pizzas = ?**)? So we have the following
Meaning | Variable
:------ | :--------:
Number of people | people
Average number of slices each person eats | avg
Number of slices on a piece of pizza | slices
"""
# βββ‘ 4dff4b5e-d461-11ea-29c8-d548fdb5f08b
md"Edit the equation below to calculate the number of pizzas to order using the variables above for **people**, **avg**, and **slices**:"
# βββ‘ 444e2fa4-d460-11ea-12aa-57e0576c2d66
pizzas = 1
# βββ‘ 3c12f2b4-d471-11ea-2d37-539f061f7cf2
r = 6
# βββ‘ d9c31dfa-d470-11ea-23b2-838975b71f7c
md"""## Writing your own math functions
The area of a pizza is ``A = \pi r^2``. Lets try calculating the area of a pizza that has a radius of $r inches (**r = $6**). Type **pi** to get the value of ``\pi`` and **r^2** to get the radius squared.
"""
# βββ‘ 50f0f6d6-d471-11ea-304e-8f72e7ef9d7e
A = r^2
# βββ‘ f907e46a-d471-11ea-07e5-f30e2aab3d08
md"""The diameter of a pizza is often stated on a menu so let's define a **formula** to calculate the area of a pizza given the diameter **d**.
We do this by writing a formula like this: `area(d) = pi * (d/2)^2`
Let's write that below:
"""
# βββ‘ cb36a9ee-d472-11ea-1835-bf7963137e18
area(d) = pi * (d / 2)^2
# βββ‘ d9575e9c-d472-11ea-1eda-2d335d039f28
md"""Now we have a function called **area** that we can pass any diameter and it will return the area of a pizza (or circle), let's try that with the pizza from before with `area(2*r)` to get the area of the pizza:
"""
# βββ‘ 04b010c0-d473-11ea-1767-136c7e26e122
A2 = area(r)
# βββ‘ edb95b14-d473-11ea-3a5a-77382d31f941
md"""## Finding the best pizza deal
Let's see if a larger pizza is a better value by calculating the price per area. There are 4 sizes: small, medium, large, extra large with the following prices:
Size | Diameter (inches) | Price ($)
:------- | :---------------: | --------:
small | 9 | 13.10
medium | 13 | 20.95
large | 15 | 24.90
XL | 17 | 30.95
### 1. How many small pizzas is the same as one XL pizza?
Edit the expression below:
"""
# βββ‘ 637c26fa-d475-11ea-2c5b-2b0f4775b119
smalls_in_xl = 1
# βββ‘ 5b07b8fe-d475-11ea-01aa-6b88d6ed8a05
md"""### 2. Calculate the cost per area of each pizza:
"""
# βββ‘ 3823d09e-d474-11ea-194e-59b5805f303b
small = 13.10 / area(9)
# βββ‘ 76c11174-d474-11ea-29c5-81856d47cf74
medium = 20.95 / area(13)
# βββ‘ 8b12d200-d474-11ea-3035-01eccf39f917
large = 24.90 / area(15)
# βββ‘ 962e6b86-d474-11ea-11a6-a1d11e33ae42
xl = 30.95 / area(17)
# βββ‘ a42e4eb0-d474-11ea-316a-3d864451bc01
md"Which size of pizza is the best deal? Write your answer below and assign it to the variable **best_value**."
# βββ‘ 16ec3f32-d4ff-11ea-20e2-5bc6dd5db083
best_value = small
# βββ‘ cb419286-d4ff-11ea-1d7f-af5c8574b775
md"""### 3. Is this a good deal?
San Marinos has a special **\"Buy two medium pizzas and save \$5\"**. Is this a better deal than buying a extra-large pizza?
Calculate the total cost of two medium pizzas deal (saving \$5):"""
# βββ‘ f147b6cc-d4ff-11ea-05ad-6f5b441e5d1b
two_medium_cost = 20.95 * 1 - 0
# βββ‘ 0d76d97c-d500-11ea-2433-e96c6fc43b05
md"Calculate the total area of two medium pizzas:"
# βββ‘ 19eb2a82-d500-11ea-3782-596adc689382
two_medium_area = 1 * area(13)
# βββ‘ 20a1e9cc-d500-11ea-3d9b-279c71bc20f1
md"Now calculate cost per area by taking the total cost of two medium pizzas and divide by the total area:"
# βββ‘ 70e85498-d500-11ea-35af-474574f5c011
two_medium_deal = 1
# βββ‘ 57f024ae-d500-11ea-1cc4-ed28348fdf93
md"""Is it a better deal to get two medium pizzas for \$5 off or to just buy an extra-large?"""
# βββ‘ 180c8fdc-d503-11ea-04ca-bf2c07fd1c17
md"""### 4. Advanced Problem
A new worker at a pizza shop was getting paid for cutting pizza into pieces. The pieces of pizza could be any size. Calculate the maximum number of pieces the worker could make with two cuts of the pizza."""
# βββ‘ 6494e270-d503-11ea-38a7-df96e7f0a241
cuts2 = 1
# βββ‘ 92b4a012-d503-11ea-15a2-1f3a446d3284
md"Now what about 3 cuts across the pizza? What is the maximum number of pieces that can be made with **3 cuts**?"
# βββ‘ a05aae8e-d506-11ea-190f-57e9ce53b8b9
cuts3 = 1
# βββ‘ 2eb9a560-d507-11ea-3b8b-9d06678fe131
md"Now, how many pieces can be made with **4 cuts**?"
# βββ‘ 5a8ede88-d507-11ea-30d9-c99a67243781
cuts4 = 1
# βββ‘ d1e3dec0-d507-11ea-1213-d37a9325ee2f
md"Are you starting to see a pattern? Can you figure out a formula for how many pieces of pizza can be made with \"n\" cuts? Make a table and fill in the number of pieces for a number of cuts and see if you can find the pattern:
Cuts | Pieces
:--- | ------:
0 | 1
1 | 2
2 | 4
3 |
4 |
"
# βββ‘ 97bfd13c-dcc2-11ea-0067-ad8c2c6517fc
md"To get an extra hint, figure out how many slices we can get from **5 cuts**:"
# βββ‘ bae0cb62-dcc2-11ea-0667-512e1c407d40
cuts5 = 1
# βββ‘ e0cb2822-dcc2-11ea-2c85-5748bfe526dc
md"Have you found the pattern? Write down the formula below:"
# βββ‘ f5f89724-d507-11ea-0a93-6d904f36bbe4
function pieces(n)
return n
end
# βββ‘ 03249876-d508-11ea-16bb-fd5afed37a1f
md"""##### Let's test your formula!"""
# βββ‘ bd9f3d24-d509-11ea-165d-3d465a0b4542
md"""Move the slider to change the number of cuts:
$(@bind n html"<input type=range max=50>")"""
# βββ‘ b8644fb0-daa6-11ea-1e94-9bf46e7b0fad
hint(text) = Markdown.MD(Markdown.Admonition("hint", "Hint", [text]));
# βββ‘ 8700d986-d475-11ea-0d0e-790448cf92ba
let
ans = (pi * (17 / 2)^2) / (pi * (9 / 2)^2)
if smalls_in_xl == 1
hint(md"""The diameter of the XL pizza is 17 inches while the diameter of the small pizza is 9 inches. Use the **area()** function from before to find the area of each and divide them.""")
elseif smalls_in_xl < ans - 4 * eps(ans)
md"""Keep trying, your answer is too low."""
elseif smalls_in_xl > ans + 4 * eps(ans)
md"""Keep trying, your answer is too high."""
else
md"""**Great!** You got it right. Let's move on."""
end
end
# βββ‘ 2814a1d4-dcc0-11ea-3d42-f52765e478fe
hint(md"For each extra cut, start out with the solution for the previous number. When you add one extra cut, how many new slices do you get?")
# βββ‘ 48647ab2-daa5-11ea-0494-ef87be7cbf7c
hint(md"A new cut will create the maximum number of _new slices_ if it intersects all previous cuts.")
# βββ‘ 8cada086-daa5-11ea-220c-0f660938b604
if cuts5 == 5 + 4 + 3 + 2 + 1 + 1
hint(md"To get the maximum number of pieces with 5 cuts it will be ``5 + 4 + 3 + 2 + 1``, plus 1 extra for the original pizza with 0 cuts. To find the formula of a sequence of numbers group them like so: ``5 + (4 + 1) + (3 + 2) = 3 * 5``.")
else
md""
end
# βββ‘ 4119d19e-dcbc-11ea-3ec8-271e88e1afca
almost(text) = Markdown.MD(Markdown.Admonition("warning", "Almost there!", [text]));
# βββ‘ 921bba30-dcbc-11ea-13c3-87554722da8a
keep_working(text=md"The answer is not quite right.") = Markdown.MD(Markdown.Admonition("danger", "Keep working on it!", [text]));
# βββ‘ 5a6d1a8e-dcbc-11ea-272a-6f769c8d309c
correct(text=md"Great! You got the right answer! Let's move on to the next section.") = Markdown.MD(Markdown.Admonition("correct", "Got it!", [text]));
# βββ‘ 33b1975c-d45c-11ea-035f-ab76e46a31ed
if c == a * b
correct(md"""**Great!** The value of c = $c. So you now have a simple computer!
Now go back above and change the value of **a = $a** to **a = $(a + 3)** and press **`Shift-Enter`**.
What is the new value of **c**? Notice how all the values get updated in this notebook!
""")
else
keep_working()
end
# βββ‘ f26d50da-d46b-11ea-0c2d-77ca13532b3d
if pizzas == people * avg / slices
almost(md"Yes that is right! But we should round $pizzas up to an integer, otherwise the restaurant will be confused.
Try `ceil(...)`!")
elseif pizzas == ceil(people * avg / slices)
correct(md"Yes that is right, that's a lot of pizza! Excellent, you figured out we need to round up the number of pizzas!")
else
keep_working()
end
# βββ‘ 5c4a5f22-d471-11ea-260f-9338d8bfa2d6
if A != pi * r^2
keep_working(md"Let's fix the above cell before we move on! Find the formula to calculate the area using **pi** and **r**.")
else
correct(md"""**Great!** You figured it out. Keep going.""")
end
# βββ‘ a07e5c3e-d476-11ea-308c-718f8f128334
if A2 != pi * (12 / 2)^2
hint(md"Keep trying to get the right answer.
**Hint**: you need to multiply the radius by 2 to convert it into the diameter.")
else
correct()
end
# βββ‘ 1ba2c208-d4ff-11ea-0a8e-e75bf7e1c3e6
if !isapprox(best_value, xl)
hint(md"No need to copy these digits yourself - what should we assign to **best_value**?")
else
correct()
end
# βββ‘ 6ae748b2-d503-11ea-1c51-6b2df24fd212
if cuts2 != 4
hint(md"The cuts must go all the way across the pizza!")
else
correct(md"Awesome!")
end
# βββ‘ a679bddc-d506-11ea-143a-6d4dcd70e918
if cuts3 == 6
almost(md"""Close but not quite. Who said that pizza slices need to look like pizza slices?""")
elseif cuts3 == 7
correct(md"You got it right. Now for something harder...")
else
hint(md"Try drawing it out on a piece of paper.")
end
# βββ‘ 5df7eefc-d507-11ea-0d1f-45b224a04774
if cuts4 == 11
correct(md"That was a tough question. How did you figure it out? You tried hard.")
elseif cuts4 < 10
hint(md"Draw it out on a piece of paper. You can make more pieces with 4 cuts.")
elseif cuts4 < 11
hint(md"Getting close but you can make more pieces with 4 cuts.")
else
hint(md"That is too high. Only straight lines!")
end
# βββ‘ e80986c6-d509-11ea-12e3-f79a54b5ab31
if pieces(n) == n * (n + 1) / 2 + 1
md"""_Testing..._
**For $n cuts, you predict $(pieces(n)) pieces.**
$(correct(md"Well done!"))"""
else
md"""_Testing..._
**For $n cuts, you predict $(pieces(n)) pieces.**
$(keep_working(md"The answer should be $(Int(n*(n+1)/2+1))."))"""
end
# βββ‘ Cell order:
# ββ03664f5c-d45c-11ea-21b6-91cd647a07aa
# β β14158eb0-d45c-11ea-088f-330e45412320
# β β2ed4bb92-d45c-11ea-0b31-2d8e32ce7b44
# ββe5e0a0da-d45c-11ea-1042-e9b5d0654d4f
# β β30f0f882-d45c-11ea-2adc-7d84ecf8a7a6
# ββ33b1975c-d45c-11ea-035f-ab76e46a31ed
# ββa38cb92e-d45e-11ea-2959-05be909befb2
# β β262b312a-d460-11ea-26c5-df30459effc5
# β β2ea7f162-d460-11ea-0e8e-25340e2e64da
# β β3da812c6-d460-11ea-0170-79fbb6a4347c
# ββ4dff4b5e-d461-11ea-29c8-d548fdb5f08b
# β β444e2fa4-d460-11ea-12aa-57e0576c2d66
# ββf26d50da-d46b-11ea-0c2d-77ca13532b3d
# ββd9c31dfa-d470-11ea-23b2-838975b71f7c
# β β3c12f2b4-d471-11ea-2d37-539f061f7cf2
# β β50f0f6d6-d471-11ea-304e-8f72e7ef9d7e
# ββ5c4a5f22-d471-11ea-260f-9338d8bfa2d6
# ββf907e46a-d471-11ea-07e5-f30e2aab3d08
# β βcb36a9ee-d472-11ea-1835-bf7963137e18
# ββd9575e9c-d472-11ea-1eda-2d335d039f28
# β β04b010c0-d473-11ea-1767-136c7e26e122
# ββa07e5c3e-d476-11ea-308c-718f8f128334
# ββedb95b14-d473-11ea-3a5a-77382d31f941
# β β637c26fa-d475-11ea-2c5b-2b0f4775b119
# ββ8700d986-d475-11ea-0d0e-790448cf92ba
# ββ5b07b8fe-d475-11ea-01aa-6b88d6ed8a05
# β β3823d09e-d474-11ea-194e-59b5805f303b
# β β76c11174-d474-11ea-29c5-81856d47cf74
# β β8b12d200-d474-11ea-3035-01eccf39f917
# β β962e6b86-d474-11ea-11a6-a1d11e33ae42
# ββa42e4eb0-d474-11ea-316a-3d864451bc01
# β β16ec3f32-d4ff-11ea-20e2-5bc6dd5db083
# ββ1ba2c208-d4ff-11ea-0a8e-e75bf7e1c3e6
# ββcb419286-d4ff-11ea-1d7f-af5c8574b775
# β βf147b6cc-d4ff-11ea-05ad-6f5b441e5d1b
# ββ0d76d97c-d500-11ea-2433-e96c6fc43b05
# β β19eb2a82-d500-11ea-3782-596adc689382
# ββ20a1e9cc-d500-11ea-3d9b-279c71bc20f1
# β β70e85498-d500-11ea-35af-474574f5c011
# ββ57f024ae-d500-11ea-1cc4-ed28348fdf93
# ββ180c8fdc-d503-11ea-04ca-bf2c07fd1c17
# β β6494e270-d503-11ea-38a7-df96e7f0a241
# ββ6ae748b2-d503-11ea-1c51-6b2df24fd212
# ββ92b4a012-d503-11ea-15a2-1f3a446d3284
# β βa05aae8e-d506-11ea-190f-57e9ce53b8b9
# ββa679bddc-d506-11ea-143a-6d4dcd70e918
# ββ2eb9a560-d507-11ea-3b8b-9d06678fe131
# β β5a8ede88-d507-11ea-30d9-c99a67243781
# ββ5df7eefc-d507-11ea-0d1f-45b224a04774
# ββd1e3dec0-d507-11ea-1213-d37a9325ee2f
# ββ2814a1d4-dcc0-11ea-3d42-f52765e478fe
# ββ48647ab2-daa5-11ea-0494-ef87be7cbf7c
# ββ97bfd13c-dcc2-11ea-0067-ad8c2c6517fc
# β βbae0cb62-dcc2-11ea-0667-512e1c407d40
# ββ8cada086-daa5-11ea-220c-0f660938b604
# ββe0cb2822-dcc2-11ea-2c85-5748bfe526dc
# β βf5f89724-d507-11ea-0a93-6d904f36bbe4
# ββ03249876-d508-11ea-16bb-fd5afed37a1f
# ββbd9f3d24-d509-11ea-165d-3d465a0b4542
# ββe80986c6-d509-11ea-12e3-f79a54b5ab31
# ββb8644fb0-daa6-11ea-1e94-9bf46e7b0fad
# ββ4119d19e-dcbc-11ea-3ec8-271e88e1afca
# ββ921bba30-dcbc-11ea-13c3-87554722da8a
# ββ5a6d1a8e-dcbc-11ea-272a-6f769c8d309c