Brainfuck Code Generator
Generate optimized Brainfuck code from any text input. Converts ASCII characters to compact Brainfuck instructions with delta encoding.
About
Brainfuck operates on a tape of 30000 byte cells with only 8 commands: + - > < [ ] . ,. Encoding text naively produces extremely long output. A character like z (ASCII 122) would need 122 consecutive + operations. This tool applies loop-based multiplication and inter-character delta encoding to reduce output length by a factor of 5Γ to 10Γ. It computes the optimal loop base b such that b Γ k approximates each target ASCII value, then emits the minimal residual adjustment. Consecutive characters reuse the current cell value, encoding only the difference Ξ΄ = ci β ciβ1. The generator handles all printable ASCII (codes 32 - 126) and common control characters like newline (ASCII 10). Note: this tool approximates optimal Brainfuck. Truly minimal Brainfuck generation is NP-hard in the general case.
Formulas
The generator uses two encoding strategies per character and selects the shorter result.
Strategy 1 - Absolute Encoding: For a target ASCII value c, find the integer factor pair (a, b) where a Γ b ≈ c and a + b is minimized. The residual is:
r = c β (a Γ b)
Output: a repetitions of +, then the loop [> b repetitions of + <-]>, then |r| repetitions of + or -, then .
Strategy 2 - Delta Encoding: For consecutive characters ciβ1 and ci, compute:
Ξ΄ = ci β ciβ1
If |Ξ΄| is small enough that |Ξ΄| + 1 (for the .) is shorter than the absolute encoding, emit |Ξ΄| repetitions of + or - followed by ..
Compression ratio:
R = LbfLtext
Where Lbf is the length of the generated Brainfuck code and Ltext is the length of the input text. Typical ratios range from 8 to 20 characters of Brainfuck per input character.
Reference Data
| Character | ASCII Code | Naive Length | Optimized Length (approx.) | Brainfuck Pattern |
|---|---|---|---|---|
| A | 65 | 67 | ~18 | ++++++++++[>++++++<-]>+++++. |
| Z | 90 | 92 | ~18 | ++++++++++[>+++++++++<-]>. |
| a | 97 | 99 | ~20 | ++++++++++[>++++++++++<-]>---. |
| z | 122 | 124 | ~22 | ++++++++++[>++++++++++++<-]>++. |
| 0 | 48 | 50 | ~16 | +++++++[>+++++++<-]>-. |
| 9 | 57 | 59 | ~18 | ++++++[>++++++++++<-]>---. |
| (space) | 32 | 34 | ~14 | ++++++++[>++++<-]>. |
| ! | 33 | 35 | ~14 | ++++++++[>++++<-]>+. |
| \n (newline) | 10 | 12 | ~10 | ++++++++++. |
| ~ | 126 | 128 | ~22 | ++++++++++[>+++++++++++++<-]>-. |
| H | 72 | 74 | ~18 | +++++++++[>++++++++<-]>. |
| e | 101 | 103 | ~20 | ++++++++++[>++++++++++<-]>+. |
| l | 108 | 110 | ~20 | +++++++++[>++++++++++++<-]>. |
| o | 111 | 113 | ~20 | ++++++++++[>+++++++++++<-]>+. |
| W | 87 | 89 | ~18 | +++++++++[>++++++++++<-]>---. |