13. Numbers and math — Homework solutions

The .lua solution files are in exercises/13/homework/solutions/.

Problem 1 — Rectangle area

Problem. Multiply width and height; print the result with a label.

How to think about it. Multiplication is *. Store the product in a third variable, or use it directly inside print with ...

Worked solution.

local width = 6
local height = 4

local area = width * height
print("Area: " .. area)

Common mistakes.

  • Writing width x height or width times height. Neither is Lua; the multiplication operator is *.

Problem 2 — Floor and ceil

Problem. Show 3.7, math.floor(3.7), and math.ceil(3.7) on three lines, plus a comment explaining the difference.

How to think about it. floor rounds down; ceil rounds up. Both return integers. For positive numbers, floor drops the decimals; ceil adds one unless the number is already whole.

Worked solution.

local x = 3.7

print(x)              -- 3.7
print(math.floor(x))  -- 3
print(math.ceil(x))   -- 4

-- floor rounds DOWN to the next integer; ceil rounds UP. For 3.7
-- they sit on either side: 3 and 4.

Common mistakes.

  • Expecting math.floor to modify x. It does not. Like string.upper, it returns a new value and leaves the original alone.

Problem 3 — Roll two dice

Problem. Roll two six-sided dice independently. Print each plus the total.

How to think about it. math.random(6) gives a number from 1 to 6. Call it twice — once per die — then add the two variables.

Worked solution.

local die1 = math.random(6)
local die2 = math.random(6)
local total = die1 + die2

print("Die 1: " .. die1)
print("Die 2: " .. die2)
print("Total: " .. total)

Common mistakes.

  • Calling math.random(6) once and re-using the number for both dice. The two rolls must be separate calls.
  • Being surprised that math.random(1, 6) and math.random(6) match. They do — both include both ends. Pick one and stay consistent.

Challenge — Hypotenuse

Problem. Given two short sides of a right triangle, compute the hypotenuse with math.sqrt, and print all three values with the hypotenuse rounded to two decimals.

How to think about it. The maths is c = sqrt(a*a + b*b). Both a*a and a^2 work, but a*a stays an integer if a is one, while ^ always gives a float — and math.sqrt does too. For rounded display, use string.format with %.2f.

Worked solution.

local a = 3
local b = 4

local c = math.sqrt(a * a + b * b)

print("a = " .. a)
print("b = " .. b)
print(string.format("c = %.2f", c))

For a = 3, b = 4 the output is:

a = 3
b = 4
c = 5.00

Common mistakes.

  • Writing math.sqrt(a^2 + b^2) and getting 5.0 instead of 5.00. The maths is correct; formatting controls the decimals. %.2f always shows two.
  • Forgetting that ^ makes everything a float: 2 ^ 2 is 4.0, not 4. Use a * a if you need an integer.

Done?

Two chapters to go before the mini-project: Working with text lets you slice, search, and replace inside strings, and Getting input turns the keyboard into values. Then the character sheet project ties Part 3 together.