Printing multiplication table to 12 × 12 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

&gt;++++[&lt;+++++ +++&gt;-]                                 set cell 0 to space
+++++ +++++                                         set cell 1 to new line

&gt;+++++ +++++ ++                                     set counter to 12
[                                                   while counter is not 0
    &gt;+                                              increment number
    &gt;+++++ +++++ ++                                 set inner counter to 12
    [                                               while inner counter is not 0
        &lt;[&gt;&gt;+&gt;+&lt;&lt;&lt;-]&gt;&gt;&gt;[&lt;&lt;&lt;+&gt;&gt;&gt;-]                   increment inner number

        &gt;++++++++++&lt;&lt;[-&gt;+&gt;-[&gt;+&gt;&gt;]&gt;[+[-&lt;+&gt;]&gt;+&gt;&gt;]
        &lt;&lt;&lt;&lt;&lt;&lt;]&gt;&gt;[-]&gt;&gt;&gt;++++++++++&lt;[-&gt;-[&gt;+&gt;&gt;]&gt;
        [+[-&lt;+&gt;]&gt;+&gt;&gt;]&lt;&lt;&lt;&lt;&lt;]&gt;[-]&gt;&gt;[&gt;++++++
        [-&lt;++++++++&gt;]&lt;.&lt;&lt;+&gt;+&gt;[-]]&lt;[&lt;[-&gt;-&lt;]++++++
        [-&gt;++++++++&lt;]&gt;.[-]]&lt;&lt;++++++[-&lt;++++++++&gt;]
        &lt;.[-]&lt;&lt;[-&lt;+&gt;]                               print number gotten off SO

        &lt;&lt;&lt;&lt;&lt;&lt;.                                     print space
        &gt;&gt;&gt;&gt;-                                       go to inner counter
    ]                                               end inner loop
    &gt;[-]&lt;                                           reset number
    &lt;&lt;&lt;.                                            print new line
    &gt;-                                              go to counter
]                                                   end outer loop


Compressed:

&gt;++++[&lt;++++++++&gt;-]++++++++++&gt;++++++++++++[&gt;+&gt;++++++++++++[&lt;[&gt;&gt;+&gt;+&lt;&lt;&lt;-]&gt;&gt;&gt;[&lt;&lt;&lt;+&gt;&gt;&gt;-]&gt;+++++++
+++&lt;&lt;[-&gt;+&gt;-[&gt;+&gt;&gt;]&gt;[+[-&lt;+&gt;]&gt;+&gt;&gt;]&lt;&lt;&lt;&lt;&lt;&lt;]&gt;&gt;[-]&gt;&gt;&gt;++++++++++&lt;[-&gt;-[&gt;+&gt;&gt;]&gt;[+[-&lt;+&gt;]&gt;+&gt;&gt;]&lt;&lt;&lt;&lt;&lt;]&gt;[-]
&gt;&gt;[&gt;++++++[-&lt;++++++++&gt;]&lt;.&lt;&lt;+&gt;+&gt;[-]]&lt;[&lt;[-&gt;-&lt;]++++++[-&gt;++++++++&lt;]&gt;.[-]]&lt;&lt;++++++[-&lt;++++++++&gt;]&lt;
.[-]&lt;&lt;[-&lt;+&gt;]&lt;&lt;&lt;&lt;&lt;&lt;.&gt;&gt;&gt;&gt;-]&gt;[-]&lt;&lt;&lt;&lt;.&gt;-]


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 72 
7 14 21 28 35 42 49 56 63 70 77 84 
8 16 24 32 40 48 56 64 72 80 88 96 
9 18 27 36 45 54 63 72 81 90 99 108 
10 20 30 40 50 60 70 80 90 100 110 120 
11 22 33 44 55 66 77 88 99 110 121 132 
12 24 36 48 60 72 84 96 108 120 132 144 


Some notes:


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.
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?


Concerns:


Does it make sense? (Considering it's Brainfuck, it shouldn't make sense...)
Do my "comments" make sense?
Are there redundant commands that I can remove/reduce?
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:

        &gt;++++++++++&lt;&lt;[-&gt;+&gt;-[&gt;+&gt;&gt;]&gt;[+[-&lt;+&gt;]&gt;+&gt;&gt;]
        &lt;&lt;&lt;&lt;&lt;&lt;]&gt;&gt;[-]&gt;&gt;&gt;++++++++++&lt;[-&gt;-[&gt;+&gt;&gt;]&gt;
        [+[-&lt;+&gt;]&gt;+&gt;&gt;]&lt;&lt;&lt;&lt;&lt;]&gt;[-]&gt;&gt;

&gt;&gt;&gt;+&lt;&lt;&lt;[

    [x&gt;++++++[-&lt;++++++++&gt;]&lt;.&lt;&lt;+&gt;+&gt;[-]]

&gt;&gt;&gt;-&lt;&lt;&lt;]&gt;&gt;&gt;[++++++++++ ++++++++++ ++++++++++ + . [-]]&lt;&lt;&lt;

&lt;

&gt;&gt;&gt;+&lt;&lt;&lt;[
    [y&lt;[-&gt;-&lt;]++++++[-&gt;++++++++&lt;]&gt;.[-]]
&gt;&gt;&gt;-&lt;&lt;&lt;]&gt;&gt;&gt;[++++++++++ ++++++++++ ++++++++++ + . [-]]&lt;&lt;&lt;


&lt;&lt;++++++[-&lt;++++++++&gt;]
        &lt;.[-]&lt;&lt;[-&lt;+&gt;]                               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.