Building numbers
By the end of this lesson you can make any number with a short program, and print a word of your own choice.
Eight times nine
A letter needs a number like 72 or 105, and typing that many plus signs is long. This program of 24 commands prints the letter H, number 72. Press Play, or drag the slider through the steps, and watch cell 0 and cell 1 while the loop runs:
Notice that every round took 1 off cell 0 and put 9 onto cell 1, and that the loop stopped when cell 0 reached 0. Eight rounds of 9 make 72, the number of the letter H. Typing 72 plus signs needs 73 commands with the full stop; this program needs 24.
This is a loop that multiplies. A counter sits in one cell, the loop adds a fixed number to the next cell every round, and a step to the right then reaches the result. If your number is not a neat product, add or take off the rest after the loop.
Sixty-five
Now you build the number. Write a program that prints the letter A, which is number 65, with at most 30 commands:
Hint
65 is not a product of two handy numbers. But 64 is.
Hint
8 × 8 = 64. One more plus after the loop makes 65.
For example ++++++++[>++++++++<−]>+.: eight eights, and one more. There are many right answers; 5 × 13 works too.
Looking for the shortest program is a sport called code golf. The Challenges keep score for you, if you like that.
Hi
The box holds the program of the first figure, which prints H. Add to it so that it prints Hi. The small letter i is number 105:
Hint
After printing, cell 1 still holds 72. How far is 105 from 72?
Hint
33 more. Thirty-three plus signs are fine here.
From 72 to 105 is 33 up. You can walk there with 33 plus signs, or run a second loop that adds to the same cell. What you should not do is start again from zero: the cell already holds a number near the one you need.
That is the whole art of printing text in brainfuck: go from each character to the next by a short way.
Your own word
Type a word in the first box, then write a program that prints exactly that word. The chart of Lesson 3 gives the numbers:
Hint
For each next letter, look up its number in the chart of Lesson 3 and work out how far it is from the letter before.
Hint
A capital and a small letter are far apart. For a big jump use a loop again: count rounds in the empty cell on the left, and add to the letter cell.
You have written a program that says something nobody told you to say. Every text a brainfuck program prints is made like this: a cell that holds a number near the next letter, and a walk from letter to letter.
The last hint writes a program for your word by itself. It is not the shortest possible; can you beat it? The classic first program in any language prints “Hello, World!”, and it is one of the Challenges: Hello, World!
Check yourself
A free exercise
Print your word with an exclamation mark (number 33) after it. Or print two words with a space (number 32) between them. Nothing is checked here:
What you can do now
You can build a number with a loop that multiplies, and print a text by walking from each character to the next. No new command came in this lesson: it was the loop of Lesson 4 put to work.