Loading [Contrib]/a11y/accessibility-menu.js

BrainFuck part 4 - Advanced maths



Welcome to this new playground about the BF language. Make sure you already read

playgrounds if you didn't already !

The goal of this playground is to play with maths a bit more. Let's start with something simple : read a number.

In all the previous playground steps, data were read as "chars", mapped to ASCII values, but what if we want to type 123 and have a value 123 (instead of 3 chars)?

Let's start

  • Memory: 0, 0, 0, 0
  • Cursor: on first cell
  • Input: a decimal number


  • invariant : first cell is the actually read number
  • Read next digit on second cell
  • While a digit has been read
    • Convert char into digit (-48)
    • Multiply number of first cell by ten
    • Add second cell to first
      • More exactly : add first cell 9 times to the second (needs less extra memory)
      • then add second to first
  • loop
  • first cell : the decimal number


>,                      read next digit on second cell
[                       while digit is read
  >++++++[-<-------->]  convert into digit
  >+++++++++[           do 9 times
    <<<[->+>+<<]        add first cell to second and create a save copy
    >>[-<<+>>]          put save copy back on first cell
  >-]                   loop (9 times)
  <<[-<+>],             add second to first then read next digit
]<                      loop

Minified version


Final state

  • Memory: N, 0, 0, 0
  • Cursor: on N
  • Input: empty
  • Output: unchanged

Test program

This very basic program reads ASCII code and prints it

>,[>++++++[-<-------->]>+++++++++[<  read number
<<[->+>+<<]>>[-<<+>>]>-]<<[-<+>],]<  ** part 2 **
.                                    print as ASCII code

Try with inputs from 65 to 90 for example to have all uppercased letters

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
Online Participants