Code patternsAdvanced: wrapping cells of a known widthWrite many numbers compactly as digits

Write many numbers compactly as digits

Puts a long row of fixed numbers on the tape with a few characters per number, instead of one plus sign per unit.

Watch it run

The form “Three digit places in base 5” works on 10 cells; the named ones are “number 0”, “number 1”, “number 2”, “number 3”, “number 4” and “number 5”. Press Play, drag the step slider forwards and back, or edit the numbers (13 to 262):

The figure needs JavaScript. The program below is the result for the example values.

A number written as plus signs costs its own value in characters. Written in digits it costs a few characters per digit: 200 is 1, 3, 0 in base 5 with digits from −2 to 2.

The row is written one digit place at a time, for all numbers at once. The text walks along the row and gives every cell its small top digit. Then one loop, [[-<+++++>]>], multiplies every cell by 5 into its left neighbour. Then the next digit is added, and so on.

That loop tests the cell it stands on to know whether the row has ended. So no cell may be zero on the way: every number starts at 1, and the digits are chosen with that in mind.

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:

>>>>+>+>+>+>+>++++++++++<++++<<+++++++<++<+++[[-<+++++>]>]<<<<+<<--<[[-<+++++>]>]<<<++<+<<-<[[-<+>]>]
Open in the playground101 characters

What it needs

Common mistakes
  • A number that passes through zero between two digit places: the multiplying loop stops there and the rest of the row is lost.
  • Thinking this saves steps. It saves characters; the steps are about the same.

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
Table 1. The cost as a function of the values. Exact formulas are checked on every case they run.
FormPlain stepsOptimised steps
Three digit places in base 5about 7 per unit of every numberabout 9 per number
The naive way: one plus per unitthe sum of the numbers1 per number
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
Three digit places in base 5a = 100, 64, 200, 31, 127, 250161015,64854
Three digit places in base 5a = 262, 262, 262, 262, 262, … (12)1625522,747122
The naive way: one plus per unita = 100, 64, 200, 31, 127, 250167787786
The naive way: one plus per unita = 262, 262, 262, 262, 262, … (12)163,1563,15612

Against the naive way

The same row written with one plus sign per unit. Look at the length of the text.

The numbers, row by row
this pattern, characters
255the naive way, characters
3,156

This pattern against the naive way, on 16-bit cells, measured when this page was written. The bars show the last row: the length of the two program texts.

ValuesOptimised stepsPlain stepsCharacters
patternnaivepatternnaivepatternnaive
a = 100, 64, 200, 31, 127, 2505465,648778101778
a = 262, 262, 262, 262, 262, … (12)1221222,7473,1562553,156

Other forms

to measure against The naive way: one plus per unit

Short to explain, long to write.

>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Open in the playground778 characters
Run this form step by step

The form “The naive way: one plus per unit” works on 7 cells; the named ones are “number 0”, “number 1”, “number 2”, “number 3”, “number 4” and “number 5”. Press Play, drag the step slider forwards and back, or edit the numbers (13 to 262):

The figure needs JavaScript. The program below is the result for the example values.

Where it is used

Sources

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.