How can i add a+b in brainfuck?

164 views Asked by At

How can i sum a and b without removing them?

  • Example we have [A,B] and i want [A,B,A+B] and not [A+B,0]?
;>;
<
[->>+>+<<<]
>
[->+>>+<<<]
>>
[<<<+>>>]
>
[<<<+>>>]
<<:

I have tried this, but "Error: Limit of operations (100000) reached before program finished!"

2

There are 2 answers

0
Mika Lammi On BEST ANSWER

You need to move A and B at the same time as adding to C if you want to keep them.

+++>        A (location 0) = 3
++++>       B (location 1) = 4
            C (location 2) = A plus B
<<          goto A
[>>+>+<<<-] add to C and location after C
>           goto B
[>+>>+<<<-] add to C and location after copy of A
>           goto C

Now your memory looks like this: 0, 0, C, A, B

If you want to keep the original locations, you need to move the A and B values back to their original positions.

>           goto copy of A
[<<<+>>>-]  move to original A
>           goto copy of B
[<<<+>>>-]  move to original B
0
Daniel Cristofani On

There isn't a ';' or ':' command in brainfuck; you're using some expanded brainfuck-based language. But apart from that, your loops in lines 7 and 9 have no way to terminate because they don't decrement the cell they keep returning to.