My attempts at exercism.org
1# Pizza Pi
2
3Welcome to Pizza Pi on Exercism's PHP Track.
4If you need help running the tests or submitting your code, check out `HELP.md`.
5If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
6
7## Introduction
8
9## Integers
10
11Integers are whole numbers that belong to the set `{..., -2, -1, 0, 1, 2, ...}`.
12The maximum and minimum values of an integer is determined by the system PHP runs in.
13Typically for most modern systems, the integer is a 64-bit value -- meaning it can represent `-9_223_372_036_854_775_808` to `9_223_372_036_854_775_807` inclusive.
14
15```php
16<?php
17
18$a = 1234;
19$a = 1_234_567;
20```
21
22You can write integer literal values in other than base 10 using a base prefix:
23
24```php
25<?php
26
27$a = 0123; // octal number (equivalent to 83 decimal)
28$a = 0o123; // octal number (as of PHP 8.1.0)
29$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
30$a = 0b11111111; // binary number (equivalent to 255 decimal)
31```
32
33> Taken from PHP's [documentation][syntax]
34
35## Floating Point Numbers
36
37Floating point numbers are used in PHP to represent a subset of real numbers.
38They start with one or more digits separated by a decimal separator.
39
40```php
41<?php
42
43$a = 1.234;
44$b = 1.2e3;
45$c = 7E-10;
46$d = 1_234.567; // as of PHP 7.4.0
47```
48
49### Common pitfalls
50
51Not all numbers and arithmetic operations can be performed with floating point numbers.
52Using floating point numbers to represent dollars and cents can lead to rounding errors.
53
54```php
55<?php
56
57$result = 0.1 + 0.2; // => 0.30000000000000004
58```
59
60## Arithmetic Operators
61
62PHP provides a number of operators for performing arithmetic operations. PHP follows the standard mathematical order of operations for its arithmetic operators. The operators that are provided by PHP are:
63
64* Identity (+)
65* Negation (-)
66* Addition (+)
67* Subtraction (-)
68* Multiplication (*)
69* Division (/)
70* Modulo (%)
71* Exponentiation (**)
72
73```php
74$moles = +'10';
75$aLotOfMoles = 6.022 * 10**23 * $moles;
76```
77
78[syntax]: https://www.php.net/manual/en/language.types.integer.php#language.types.integer.syntax
79
80## Instructions
81
82Lilly loves cooking.
83She wants to throw a pizza party with her friends.
84To make the most of her ingredients, she needs to know how much of each ingredient she requires before starting.
85
86To solve this, Lilly has drafted a program to automate some of the planning but needs your help finishing it.
87Will you help Lilly throw the proportionally perfect pizza party?
88
89## 1. A Dough Ratio
90
91Lilly is a fan of thin, crispy pizzas with a thinner crust.
92The dough needed for the middle is a minimum 200g, but every person it serves requires another 20g of dough.
93
94`grams = pizzas * ((persons * 20) + 200)`
95
96Lilly needs a function that:
97
98- Takes the number of pizzas
99- The number of persons each pizza will serve
100- And returns the dough needed to the nearest gram.
101
102For example, to make 4 pizzas that feed 8 people:
103
104```php
105<?php
106
107$pizza_pi = new PizzaPi();
108$pizza_pi->calculateDoughRequirement(4, 8);
109// => 1440
110```
111
112## 2. A Splash of Sauce
113
114Lilly is meticulous when applying her sauce, but the size of her pizzas can be inconsistent.
115From her experience, she knows that it takes 125mL of sauce per pizza.
116Lilly needs a function that calculates how many cans of sauce to buy.
117
118`cans of sauce = pizzas * sauce per pizza / sauce can volume`
119
120For example, given Lilly needs to make 8 pizzas, and each can is 250mL:
121
122```php
123<?php
124
125$pizza_pi = new PizzaPi();
126$pizza_pi->calculateSauceRequirement(8, 250);
127// => 4
128```
129
130## 3. Some Cheese, Please
131
132Cheese comes in perfect cubes and is sold by size.
133
134The formula Lily uses is not the formula for the area of the pizza since she does _not_ want the cheese to cover the entire area.
135She decided to use the following formula to determine how many pizzas of some diameter (`diameter`) can be made from a cheese cube of some side-length (`cheese_dimension`):
136
137`pizzas = (cheese_dimension³) / (thickness * PI * diameter)`
138
139Create a function that:
140
141- Takes a side-length dimension of a cheese cube
142- Takes the desired thickness of the cheese layer
143- Takes the diameter of the pizza
144- And uses Lilly's formula to return the number of pizzas that can be made while rounding down.
145
146For example, given a 25x25x25cm cheese cube, 0.5cm thick cheese layer and pizzas 30cm in diameter:
147
148```php
149<?php
150
151$pizza_pi = new PizzaPi();
152$pizza_pi->calculateCheeseCubeCoverage(25, 0.5, 30);
153// => 331
154```
155
156## 4. A Fair Share
157
158Finally, Lilly wants her pizzas to divide into 8 slices each and distributed evenly among her friends.
159
160Create a function that:
161
162- Takes a number of pizzas and number of friends
163- and returns the number of slices that will be left over if each person takes an equal number of slices.
164
165For example:
166
167```php
168<?php
169
170$pizza_pi = new PizzaPi();
171$pizza_pi->calculateLeftOverSlices(2, 4);
172// => 0
173$pizza_pi->calculateLeftOverSlices(4, 3);
174// => 2
175```
176
177## Source
178
179### Created by
180
181- @neenjaw