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 answers

0
6godpro On

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

,>>+++ +++[<<-------->>-]
<,>+++ +++[<-------->-]
<
[<[>>+>+<<<-]
>>>
[<<<+>>>-]
<<-]
>>++++++[<++++++++>-]
<.
0
takayoshi 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
monsterbananas On

Kinda hard to understand, but it works

>[>>>+<<<-]>>>[>+>+<<-]>>[<<+>>-]<<<<<<[>+>+>+<<<-]>>>[<<<+>>>-]>>[-<<<[-<<+>>]<[>+>+<<-]>>[<<+>>-]<<>>>>]<[-]<<[-]<[-]<
1
pierokr On
,>,<[>[>+>+<<-]>>[<<+>>-]<<<-]>>.
0
Matan Perelman 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
Filip Cvejic 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
KieranSword 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
Maxwell D. Dorliea On

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

,
------------------------------------------------
>,
------------------------------------------------
<
[
>
[
>+>+<<-
]
>>
[
<<+>>-
]
<<<-
]
>>++++++++++++++++++++++++++++++++++++++++++++++++.
1
JasonS05 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.