MyNameIsKhan - 1 year ago 132

Brainfuck Question

Given an arbitrarily long number, how can I output its double? I know how to multiply small numbers together as long as the result is <10, but what about larger integers like 32984335, and doubling something like that? I don't know the right way to handle something like this.

Answer Source

This is the algorithm you need to implement:

- Start the current count with 0;
- Multiply the current count by ten: this can be achieved by dupping 10 times, and then adding all dupes together;
- Read a digit;
- If it's null proceed to 8;
- Convert it to an actual number: this can be achieved by subtracting 48;
- Add it to the current count;
- Proceed to 2;
- Duplicate the current count;
- Adding the dupes together;
- Divide by ten using repeated subtraction; keep quotient and remainder;
- Grab the remainder;
- Make it a digit (add 48);
- Print it;
- Grab the quotient from 10;
- If it's not zero, goto 10;
- The end.

All these steps consists of basic brainfuck idioms, so it should be easy to implement.