Code for multiplying two one digit numbers in Brainfuck

7.9k Views Asked by At

Can someone please post a code piece for multiplying two one-digit numbers in the programming language brainf*ck?

9

There are 9 best solutions below

0
On

Post was made 12 years ago but I’d still like to share my answer just incase someone else see this thread.

,>>+++ +++[<<-------->>-]
<,>+++ +++[<-------->-]
<
[<[>>+>+<<<-]
>>>
[<<<+>>>-]
<<-]
>>++++++[<++++++++>-]
<.
0
On
,>,< input numbers at cell #1 #2
[
 > go to cell #2
 [
   ->+>+<< move data to cell #3 #4
 ]
 >> go to cell #4
 [
  -<<+>> move data to cell #2
 ]
 <<< go to cell #1
 - decrement cell #1
]
>>. output cell #3

Program read to cell #1, #2 and result will be appear in cell #3

I use BF interpreter where I can input numbers as numbers(not ASCII Symbols)

0
On

Kinda hard to understand, but it works

>[>>>+<<<-]>>>[>+>+<<-]>>[<<+>>-]<<<<<<[>+>+>+<<<-]>>>[<<<+>>>-]>>[-<<<[-<<+>>]<[>+>+<<-]>>[<<+>>-]<<>>>>]<[-]<<[-]<[-]<
1
On
,>,<[>[>+>+<<-]>>[<<+>>-]<<<-]>>.
0
On

well, I was inspired by the first one and made it much more simple:

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

the 48+ in the end is for the bfdev to show it in ascii.

2
On

Well, I might not have the most efficient way around it, but it works. I did things in a specific ways so that it would work with all of these

2*3=6

6*7=42

4*5=20

So, here it is:

read 
>, >, <<

convert from ascii
+++++ +
[
 >----- ---
 >----- ---
 <<-
]

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

separate numbers
>[-]>+> >+++++ +++++<
[
 - >- [>>>]+++++ +++++<<+
 [<<<]>>>>
]
<-
<+++++ +++++>>>[-<<<->>>]<<<


convert to ascii
<+++++ +
[
 >+++++ +++>
 [+++++ +++>]
 <[<]>-
]

print
>>[.<<]<[<<]>>.

I used this interpreter: http://esoteric.sange.fi/brainfuck/impl/interp/i.html

0
On

I found this VERY VERY simple version that outputs the answer in the second cell ++[>++<-] This example multiplies 2 by 2 and the number of +s in the beginning and in the bracket loop are the numbers to be multiplied

0
On

I know this question is 11 years old but this is for future readers.

,
------------------------------------------------
>,
------------------------------------------------
<
[
>
[
>+>+<<-
]
>>
[
<<+>>-
]
<<<-
]
>>++++++++++++++++++++++++++++++++++++++++++++++++.
1
On

I know this was posted over eight years ago, but I’d still like to share my answer in case anyone else stumbles across this thread.

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

This uses eight cells of space which should all be initialized with zero (in case your using this in a larger program) and the pointer begins at the left most of the eight cells. It will take in two single digit ASCII numbers and output a single two digit ASCII number. By an ASCII number, I mean it will take in and output the ASCII values of the characters making up the number. When this program is done, the pointer will once again be at the left most end of the eight cells and all cells will have been returned to zero. The values this will produce on the tape in normal operation will not go below 0 or exceed 81, so you don’t need to worry about negatives or wrapping.