开发者

Code for multiplying two one digit numbers in Brainfuck

Can someone please post a code piece for multiplying two one-digit numbers in the progra开发者_如何学Pythonmming language brainf*ck?


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


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


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.


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


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.


Kinda hard to understand, but it works

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


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

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


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

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜