Learn Lua · Basics · Lesson 4 of 4

Working with numbers

Lua has whole numbers (3) and numbers with a fraction (2.5). +, - and * work as you'd expect. / always gives a fraction, // divides and rounds down, and % gives what's left over:

LUA
10 / 4   --> 2.5
10 // 4  --> 2
10 % 4   --> 2

The math library rounds: math.ceil rounds up, math.floor rounds down and math.round rounds to the nearest whole number:

LUA
math.ceil(2.5)   --> 3
math.floor(2.5)  --> 2
math.round(2.4)  --> 2

What your code receives

Your code gets its data as input. The type Input line at the top of the code lists every field, and the editor uses it to suggest field names and spot typos. Here is an example. Each check sends different values, so work from the fields rather than these exact values.

LUA
input = { pages = 10, pagesPerSheet = 4, copies = 50 }

Steps

  1. Return sheets: how many sheets one copy needs, input.pages divided by input.pagesPerSheet, rounded up.
  2. Add total: the sheets needed for all input.copies.
  3. Add blank: how many page spaces are left empty on the sheets of one copy.
Show a hint

A copy of 10 pages at 4 pages per sheet needs 3 sheets. Those 3 sheets have room for 12 pages, so 2 are blank.