Snapshot.jl
← Notebooks/Basic mathematics🏝️ 1/1 cells interactive
.jl source

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.

2
a = 2
6
b = 6

Fix the value of c below to make it c = a * b

10
c = 10

Keep 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:

OperationType 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

MeaningVariable
Number of peoplepeople
Average number of slices each person eatsavg
Number of slices on a piece of pizzaslices
10
people = 10
2.5
avg = 2.5
8
slices = 8

Edit the equation below to calculate the number of pizzas to order using the variables above for people, avg, and slices:

1
pizzas = 1

Keep 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.

6
r = 6
36
A =  r^2

Keep 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 (generic function with 1 method)
area(d) = pi * (d / 2)^2

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:

28.274333882308138
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:

SizeDiameter (inches)Price ($)
small913.10
medium1320.95
large1524.90
XL1730.95

1. How many small pizzas is the same as one XL pizza?

Edit the expression below:

1
smalls_in_xl = 1

Hint

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:

0.2059189880991436
small = 13.10 / area(9)
0.15783649977634118
medium = 20.95 / area(13)
0.14090517628402466
large = 24.90 / area(15)
0.1363555844621221
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.

0.2059189880991436
best_value = small

Hint

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):

20.95
two_medium_cost = 20.95 * 1 - 0

Calculate the total area of two medium pizzas:

132.73228961416876
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:

1
two_medium_deal = 1

Is 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.

1
cuts2 = 1

Hint

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?

1
cuts3 = 1

Hint

Try drawing it out on a piece of paper.

Now, how many pieces can be made with 4 cuts?

1
cuts4 = 1

Hint

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:

CutsPieces
01
12
24
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:

1
cuts5 = 1

Have you found the pattern? Write down the formula below:

pieces (generic function with 1 method)
function pieces(n)
	return n
end
Let'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.

Pluto notebooks as lean Therapy components β€” interactive WebAssembly, no server.