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 = 2, b = 6 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.
a = 2b = 6Fix the value of c below to make it c = a * b
c = 10Keep working on it!
The answer is not quite right.
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 10 people (people = 10) and each person wants 2.5 slices on average (avg = 2.5). A pizza has 8 slices per pizza (slices = 8). 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 |
people = 10avg = 2.5slices = 8Edit the equation below to calculate the number of pizzas to order using the variables above for people, avg, and slices:
pizzas = 1Keep working on it!
The answer is not quite right.
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 6 inches (r = 6). Type pi to get the value of $\pi$ and r^2 to get the radius squared.
r = 6A = r^2Keep working on it!
Let's fix the above cell before we move on! Find the formula to calculate the area using pi and r.
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:
area(d) = pi * (d / 2)^2Now 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:
A2 = area(r)Hint
Keep trying to get the right answer. Hint: you need to multiply the radius by 2 to convert it into the diameter.
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:
smalls_in_xl = 1Hint
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.
2. Calculate the cost per area of each pizza:
small = 13.10 / area(9)medium = 20.95 / area(13)large = 24.90 / area(15)xl = 30.95 / area(17)Which size of pizza is the best deal? Write your answer below and assign it to the variable best_value.
best_value = smallHint
No need to copy these digits yourself - what should we assign to best_value?
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):
two_medium_cost = 20.95 * 1 - 0Calculate the total area of two medium pizzas:
two_medium_area = 1 * area(13)Now calculate cost per area by taking the total cost of two medium pizzas and divide by the total area:
two_medium_deal = 1Is it a better deal to get two medium pizzas for $5 off or to just buy an extra-large?
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.
cuts2 = 1Hint
The cuts must go all the way across the pizza!
Now what about 3 cuts across the pizza? What is the maximum number of pieces that can be made with 3 cuts?
cuts3 = 1Hint
Try drawing it out on a piece of paper.
Now, how many pieces can be made with 4 cuts?
cuts4 = 1Hint
Draw it out on a piece of paper. You can make more pieces with 4 cuts.
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 |
Hint
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?
Hint
A new cut will create the maximum number of new slices if it intersects all previous cuts.
To get an extra hint, figure out how many slices we can get from 5 cuts:
cuts5 = 1Have you found the pattern? Write down the formula below:
function pieces(n)
return n
endLet's test your formula!
Move the slider to change the number of cuts:
Testing...
For 25 cuts, you predict 25 pieces.
Keep working on it!
The answer should be 326.