HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Printing multiplication table to 12 × 12

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
printingtablemultiplication

Problem

Now, a while ago, I said in chat:


I'm done with BF programming.

Except I guess I was lying. Here's a Brainfuck program I wrote, that prints out the multiplication tables up to 12. Fairly simple; what it does:

  • Loops through 1-12; let's say the current value we are at is x



  • Loop 12 times, adding x each time, and prints the result



  • Resets the number



The code:

Cell 0 is space
Cell 1 is newline
Cell 2 is outer counter
Cell 3 is outer number
Cell 4 is inner counter
Cell 5 is inner number
Cell 6 is copy workspace

>++++[-]                                 set cell 0 to space
+++++ +++++                                         set cell 1 to new line

>+++++ +++++ ++                                     set counter to 12
[                                                   while counter is not 0
    >+                                              increment number
    >+++++ +++++ ++                                 set inner counter to 12
    [                                               while inner counter is not 0
        >+>+>>[>>-]                   increment inner number

        >+++++++++++>-[>+>>]>[+[-]>+>>]
        >[-]>>>++++++++++-[>+>>]>
        [+[-]>+>>][-]>>[>++++++
        [-]+>[-]]-++++++++.[-]]]
        ]                               print number gotten off SO

        >>>-                                       go to inner counter
    ]                                               end inner loop
    >[-]-                                              go to counter
]                                                   end outer loop


Compressed:

>++++[-]++++++++++>++++++++++++[>+>++++++++++++[>+>+>>[>>-]>+++++++
++++>-[>+>>]>[+[-]>+>>]>[-]>>>++++++++++-[>+>>]>[+[-]>+>>][-]
>>[>++++++[-]+>[-]]-++++++++.[-]]]]>>>-]>[-]-]


Output:

```
1 2 3 4 5 6 7 8 9 10 11 12
2 4 6 8 10 12 14 16 18 20 22 24
3 6 9 12 15 18 21 24 27 30 33 36
4 8 12 16 20 24 28 32 36 40 44 48
5 10 15 20 25 30 35 40 45 50 55 60
6 12 18 24 30 36 42 48 54 60 66

Solution

The multiplication table is missing formatting. I realise that, and will add it some time later, if I can figure out how to compare two values.

Comparing two values isn't that complicated, once you know how to do it. It is however extremely inefficient if you do it over and over again. Your best bet would be to hook into the printing of the number and print spaces if there are no hundreds or tens.

I managed to do a little proof-of-concept for this, which I am sure can be optimized further. The idea is the following:

  • Before the loop that prints the number of 100's, set a temporary flag on another cell



  • If the loop is entered, reset the temporary flag at the end



  • Outside the loop, go to temporary flag and if it is set, print a space. Clear the flag and go back to the cell.



The unformatted and unoptimized version of this enhanced "print the number" code is as follows:

>+++++++++++>-[>+>>]>[+[-]>+>>]
        >[-]>>>++++++++++-[>+>>]>
        [+[-]>+>>][-]>>

>>>+++++++[-]+>[-]]

>>>->>[++++++++++ ++++++++++ ++++++++++ + . [-]]>>+-++++++++.[-]]
>>>->>[++++++++++ ++++++++++ ++++++++++ + . [-]]]
        ]                               print number gotten off SO


I have marked the loop where the 100's are printed with x and the loop for the 10's with y, to make it easier for you to see the changes I did.


I realise it doesn't work for multiplication tables of 16 or higher, due to BF cell sizes being only 8 bits, or up to 255. The value 16 * 16 would result in 256, setting the value of the cell to be 0. Is this unavoidable? Or is there a trick to it?

There are some different possible solutions to this:

  • Use a Brainfuck interpreter that supports higher values (Disclaimer: That's my project)



  • Print value of cell as number for any sized cell



  • Use bit-width conversions



  • Perform the multiplication differently. This would involve a completely different approach of storing each individual digit in its own cell and doing the multiplication on a more detailed level, by looping over the digits and multiplying. I won't go into much more detail about this as it would require a lot of work.



I am unfortunately only experienced with using a Brainfuck interpreter that supports higher values. I don't know at all how the Bit-width conversions work, or how you are gonna do the number printing then.


Does it make sense? (Considering it's Brainfuck, it shouldn't make sense...)


Do my "comments" make sense?

Surprisingly much sense, yes. For a Brainfuck developer your code is quite easy to follow and your comments are helpful.


Are there redundant commands that I can remove/reduce?

There are two places in your code where I am, without doing over-analysis, able to find optimization possibilities: How you structure your memory, and how you print the numbers.

When structuring the memory, think about which places you need to go, and in which order. Currently you have positioned the space (value 32) at index 0 on the tape, and yet it is one of the most accessed values. Positioning that closer to the "inner number", will allow you to go to it more easily.

The printing of the number is a common algorithm found at Stack Overflow, which is fine. But I have realized that it is a bit inefficient for printing multiple numbers, as it goes from value 0 to value 48 (ASCII value for '0') multiple times, and resets to 0 every time. It would be better to initialize a '0' into a cell and re-use that cell multiple times, without resetting it.

Code Snippets

>++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]
        <<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>
        [+[-<+>]>+>>]<<<<<]>[-]>>

>>>+<<<[

    [x>++++++[-<++++++++>]<.<<+>+>[-]]

>>>-<<<]>>>[++++++++++ ++++++++++ ++++++++++ + . [-]]<<<

<

>>>+<<<[
    [y<[->-<]++++++[->++++++++<]>.[-]]
>>>-<<<]>>>[++++++++++ ++++++++++ ++++++++++ + . [-]]<<<


<<++++++[-<++++++++>]
        <.[-]<<[-<+>]                               print number gotten off SO

Context

StackExchange Code Review Q#112194, answer score: 9

Revisions (0)

No revisions yet.