Let’s start this coding challenge with a simple question: assuming your are organising a pizza party and would like to make sure there will be enough pizza for your guests, would you rather order a single 18-inch pizza or order 2 smaller 12-inch pizzas?
The answer might surprise you but a 18″ pizza would have more pizza than two 12″ pizzas! To verify this statement let’s calculate the actual areas of our pizzas:
As we can see 81π > 72π hence an 18″ pizza will give you more pizza than two 12″ pizzas!
The Pizzaiolo’s Challenge
For this coding challenge, we will use a Python script to help a pizzaiolo determine the perfect size for their pizzas. Our pizzaiolo offers pizzas in three sizes: small, medium, and large. The medium-sized pizza has a diameter of 12 inches.
Our pizzaiolo would like to find out what the size (diameter) of a large pizza should be to to ensure that the large pizza is exactly equivalent in area to two medium-sized pizzas.
So, let’s break down the challenge and provide you with the tools you need to solve it.
Aim of this Challenge:
The challenge is to write a Python script that calculates the diameter of a large pizza such that its area is exactly equivalent to the combined area of two medium-sized 12″ pizzas.
Understanding the Problem
To solve this problem, we need to understand the relationship between the diameter and the area of a circle (which is the shape of our pizzas). The formula for the area of a circle with a diameter D is:
Given that the diameter of a medium pizza is 12 inches, we can calculate its area as follows:
Since we want the large pizza to have the same area as two medium pizzas, the area of the large pizza should be:
Solving the Challenge
There are two main approaches to solve this challenge. We could either use an iterative trial and error approach or we could use an exact algebraic calculation.
1. Using an Iterative Trial and Error Approach:
This approach involves guessing the diameter of the large pizza, calculating its area, and adjusting the guess until the area matches (72π).
Here’s a Python script that uses the iterative trial and error approach:
2. Using an Algebraic Calculation:
This approach involves using the area formula directly to solve for the diameter of the large pizza.
Here’s a Python script that uses the algebraic approach:
Your Turn: Calculating the size of a Small Pizza
Now that we’ve determined the diameter of the large pizza, let’s extend the challenge to find the diameter of the small pizza. The requirement is that three small pizzas should have the same area as two medium-sized pizzas.. Your task is to complete the code provided above, using either a trial and error approach or working out the exact algebraic formula to solve this challenge.