MyNameIsKhan MyNameIsKhan - 1 year ago 162
Brainfuck Question

Multiply a number by 2 in Brainfuck?

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:

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download