Binary Birthday Cake

Grandpa is Turning 60!

Unfortunately, the supermarket doesn’t have enough candles to fill his birthday cake! He needs five boxes of 12 to light the cake the traditional way. The store only has one box—not enough to even form the numerals “6” and “0” legibly.

Luckily, you aren’t constrained by traditional thinking!

Your job is to design and code a Scratch program that, given somebody’s age, will light a birthday cake in binary.

For example, if the person were 26 years old, the binary representation of their age is 11010.

We can see this by calculating what each binary digit represents within the overall number:

  • = 110102
  • = (1 x 24) + (1 x 23)+ (0 x 22) + (1 x 21) + (0 x 20)
  • = (1 x 16) + (1 x 8) + (0 x 4) + (1 x 2) + (0 x 1)
  • = 16 + 8 + 0 + 2 + 0
  • = 2610

The birthday cake for a 26-year-old should, therefore, have at least five candles to represent the 5 bits of the binary number 110102. If we say a lit candle represents a 1 bit and an unlit candle represents a 0 bit, our finished cake might look something like this:

Note that the example picture above includes two additional leading unlit candles; because these are unlit, they are effectively identical to leading zeroes, and as such, do not affect the value of the number represented.

Modulus

The mod block is essential to completing the program efficiently.

The modulus (mod) operator is used to return the remainder of a division operation. It might remind us how we answered division problems before we learned about decimals (e.g., 10 ÷ 3 = 3 remainder 1). Correspondingly, 10 mod 3 = 1. As you complete this assignment, consider the place value of each digit you need to change. If you count down from 25, you can isolate the 5 from 25 by performing a mod operation (25 mod 10 = 5). You can then isolate the 2 from 25 by performing the following operation:

[[25 - [25 mod 10]] mod 100] / 10

To see how this reduces to 2, note that

[[25 - [25 mod 10]] mod 100] / 10

= [[25 - 5] mod 100] / 10
= [20 mod 100] / 10
= [20] / 10 
= 2

For more explanation on the mod block, visit the Scratch Wiki mod entry.

Program Notes

  • A starter project you may remix is available here.

  • The program screen should include at least a birthday cake and candles.

  • The allowable age range for a person to enter is 0–122, as 122 is the oldest confirmed age of a human, namely Jeanne Calment—so the maximum number of candles required is seven.

  • Lastly, the easiest way to have a candle lit/unlit is to have two different “costumes”—one corresponding to each, and select the one to display based on the binary representation of the desired age.

  • (Optional) The number of candles should change depending on the age. So, a one-year-old would have one candle, a 30-year-old would have five, and an 83-year-old would have seven, because the next highest power of 2 in each case is 1, 5, and 7, respectively.