Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code for multiplying two one digit numbers in Brainfuck

Tags:

brainfuck

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

like image 925
UgaBuga Avatar asked Mar 02 '11 09:03

UgaBuga


2 Answers

,>,< 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)

like image 127
takayoshi Avatar answered Oct 22 '22 22:10

takayoshi


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

like image 6
Filip Cvejic Avatar answered Oct 22 '22 23:10

Filip Cvejic