Code patternsInput and outputPrint a constant string
Print a constant string
Prints a fixed text with as short a program as possible.
Watch it run
The form “A few cells seeded by one loop” works on 7 cells; the named ones are “counter”. Press Play, drag the step slider forwards and back, or edit the text:
The figure needs JavaScript. The program below is the result for the example values.
A letter is printed by making its code in a cell. Making each code from zero is long: "H" alone is 72 plus signs.
Shorter: keep the cell and walk from one letter's code to the next. "l" to "o" is three plus signs.
Shorter still: one loop first puts a round number in each of a few cells (near 30 for spaces, near 70 for capitals, near 100 for small letters). Each letter is then a short walk from the nearest cell. The classic "Hello World!" program is built this way.
The code
This is the program the figure ran, written for these values. It is brainfuck as it stands: copy it, or open it in the playground, where you can change it:
+++++++++++[>+++>++++>+++++++>++++++++>+++++++++>++++++++++<<<<<<-]>>>-----.>>++.>--..+++.<<<<.<-.>>>-.>>.+++.------.<-.<<<<+.
What it needs
- One cell, or a handful: one per group of letter codes. They are left holding the last letters.
- Any cell size. The text is fixed when the program is written.
Common mistakes
- Clearing between letters. The whole gain comes from keeping what is there.
- Counting on wrapping to reach a far letter the short way round: that makes the program depend on 8-bit cells.
What it costs
A plain step is one command. An optimised step is one step of an interpreter that folds runs of the same command and flat loops. Choose the values and watch the two bars; What costs what explains how cost is counted:
The figure needs JavaScript. The table below has the measured steps.
Cost as a formula, and measured
| Form | Plain steps | Optimised steps |
|---|---|---|
| A few cells seeded by one loop | the length of the program is what counts here | about 2 per letter |
| One cell, from letter to letter | the length of the program | 2 per letter |
| Every letter from zero | about 5 per unit of every letter code | 3 per letter |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| A few cells seeded by one loop | the text = "Hi" | 8 | 41 | 272 | 5 |
| A few cells seeded by one loop | the text = "Hello, World!" | 8 | 126 | 676 | 25 |
| One cell, from letter to letter | the text = "Hi" | 8 | 107 | 107 | 4 |
| One cell, from letter to letter | the text = "Hello, World!" | 8 | 366 | 366 | 25 |
| Every letter from zero | the text = "Hi" | 8 | 185 | 535 | 6 |
| Every letter from zero | the text = "Hello, World!" | 8 | 1,181 | 3,413 | 39 |
Other forms
One cell, from letter to letter
No loop at all.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++.+++++++..+++.-------------------------------------------------------------------.------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++++++++++++.+++.------.--------.-------------------------------------------------------------------.
Run this form step by step
The form “One cell, from letter to letter” works on one cell, named “letter”. Press Play, drag the step slider forwards and back, or edit the text:
The figure needs JavaScript. The program below is the result for the example values.
to measure against Every letter from zero
The naive way, to measure against.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++.[-]
Run this form step by step
The form “Every letter from zero” works on one cell, named “letter”. Press Play, drag the step slider forwards and back, or edit the text:
The figure needs JavaScript. The program below is the result for the example values.
Where it is used
- The chat program prints every word piece as a walk from the previous byte, inside a branch that is chosen by the race (see pick the largest by a race).
- Build toward a chatbot, lesson 11, "Choosing the word", prints " Paris" this way.
Sources
- Source: Wikipedia: Brainfuck, "Hello World!": the classic program that seeds four cells with one loop; the generator here is our own.
- Source: Esolang wiki: Brainfuck constants: for the shortest way to reach a single code.
Every program on this page was run on many inputs, in plain and in optimised mode, and compared with JavaScript. The explanations are written for this page.