Skip to main content
1 of 7
TheCoffeeCup
  • 9.4k
  • 4
  • 36
  • 96

Printing Multiplication Tables to 12: in BF

Now, a while ago, I said in chat:

I'm done with BF programming.

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

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

        <<<<<<.                                     print space
        >>>>-                                       go to inner counter
    ]                                               end inner loop
    >[-]<                                           reset number
    <<<.                                            print new line
    >-                                              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 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 (most likely never).
  • 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:

  1. Does it make sense? (Considering it's Brainfuck, it shouldn't make sense...)
TheCoffeeCup
  • 9.4k
  • 4
  • 36
  • 96