Adding numbers whose sum is greater than 10 in brainfuck

333 views Asked by At

Is there an efficient algorithm that determine the sum of two numbers, even if these numbers are greater than ten?

2

There are 2 answers

0
Cedric Mamo On

the user can only enter the numbers one digit at a time. Assuming only positive numbers can be entered the algorithm to parse it is as follows.

initialize accumulator to 0
for each digit the user enters
    multiply the accumulator by 10
    add the new digit to the accumulator

You will need to handle when the user enters the enter key to finish entering the number, and the above algorithm only works for numbers up to 255 (assuming 8 bit cells).

From then on you have a cell with a number. Do this again to get a cell with another number, then you can simply add them together normally.

0
I. Golsby On

Using http://fatiherikli.github.io/brainfuck-visualizer/ you can see the value of each cell numerically, not having to use all that dumb ASCII conversion stuff. Then, just program in your inputs and the algorithm.

++++++>+++++ 6 plus 5
[<+>-]

Adds one to 6 and subtracts one from 5 each iteration, making 11. The visualizer will show that the cell is 11. If you want to preserve the y value, add a "temp" cell

++++++>+++++>[-] #0: 6 #1: 5 #2: 0
<[<+>>+<-]
>[<+>-]