Brainf*ck 中的乘法
Multiplication in Brainf*ck
我试着写了一个 brainfuck 程序,要求用户输入两个数字(小于 10)来计算这些数字的乘积。计算完成后,应该打印结果。我的代码如下所示:
++++[>++++[>+++<-]<-] writing 48 / 0x30 / '0' in cell(2)
,>,> reading two numbers in cell(0) and cell(1)
[<-<->>-] decrementing cell(0) and cell(1) by 48 / 0x30 / '0'
<< go to cell(0)
[ muliplication loop
> go to cell(1)
[>+>+<<-] move cell(1) to cell(2) and cell(3)
>> go to cell(3)
[<<+>>-] move cell(3) back to cell(1)
<<<- decrement cell(0)
]
++++[>++++[>+++<-]<-] adding 48 / 0x30 / '0' to cell(2)
>>. print result
这给了我非常奇怪的结果:
0 * 1 = 3
1 * 1 = 4
1 * 2 = 8
2 * 1 = 5
2 * 2 = :
等等。
当然,输出实际上是这样的:
1
1
4
但我想在这里显示它更具可读性。
经过深思熟虑,我意识到我将结果修改为可打印数字时犯了一个巨大的错误。我使用 cell(1) 作为临时计数器单元格,尽管它仍然有价值。所以我在将 48 / 0x30 / '0'
添加到结果之前插入了 >[-]<
:
++++[>++++[>+++<-]<-] writing 48 / 0x30 / '0' in cell(2)
,>,> reading two numbers in cell(0) and cell(1)
[<-<->>-] decrementing cell(0) and cell(1) by 48 / 0x30 / '0'
<< go to cell(0)
[ mulitplication loop
> go to cell(1)
[>+>+<<-] move cell(1) to cell(2) and cell(3)
>> go to cell(3)
[<<+>>-] move cell(3) back to cell(1)
<<<- decrement cell(0)
]
>[-]< set cell(1) to 0 so that it can be used as counter
++++[>++++[>+++<-]<-] adding 48 / 0x30 / '0' to cell(2)
>>. print result
请注意,它仍然只适用于小于 10 的结果
一个非常好的资源是 the Esolangs page on Brainfuck algorithms。在那里,乘法定义为:
temp0[-]
temp1[-]
x[temp1+x-]
temp1[
y[x+temp0+y-]temp0[y+temp0-]
temp1-]
因此,如果磁带看起来大致像 temp0 temp1 x y
,指针位于 temp0
,则生成的替换将是:
[-]
>[-]
>[<+>-]
<[
>>[<+<<+>>>-]<<<[>>>+<<<-]
>-]
但是,您的主要问题出现在调用.
输出时。您的方法仅在输出个位数时有效。要将单元格的内容输出为数字,可以使用以下代码:
>++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-
<+>]>+>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++++++
<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]
(The original post containing this algorithm.)
我试着写了一个 brainfuck 程序,要求用户输入两个数字(小于 10)来计算这些数字的乘积。计算完成后,应该打印结果。我的代码如下所示:
++++[>++++[>+++<-]<-] writing 48 / 0x30 / '0' in cell(2)
,>,> reading two numbers in cell(0) and cell(1)
[<-<->>-] decrementing cell(0) and cell(1) by 48 / 0x30 / '0'
<< go to cell(0)
[ muliplication loop
> go to cell(1)
[>+>+<<-] move cell(1) to cell(2) and cell(3)
>> go to cell(3)
[<<+>>-] move cell(3) back to cell(1)
<<<- decrement cell(0)
]
++++[>++++[>+++<-]<-] adding 48 / 0x30 / '0' to cell(2)
>>. print result
这给了我非常奇怪的结果:
0 * 1 = 3
1 * 1 = 4
1 * 2 = 8
2 * 1 = 5
2 * 2 = :
等等。
当然,输出实际上是这样的:
1
1
4
但我想在这里显示它更具可读性。
经过深思熟虑,我意识到我将结果修改为可打印数字时犯了一个巨大的错误。我使用 cell(1) 作为临时计数器单元格,尽管它仍然有价值。所以我在将 48 / 0x30 / '0'
添加到结果之前插入了 >[-]<
:
++++[>++++[>+++<-]<-] writing 48 / 0x30 / '0' in cell(2)
,>,> reading two numbers in cell(0) and cell(1)
[<-<->>-] decrementing cell(0) and cell(1) by 48 / 0x30 / '0'
<< go to cell(0)
[ mulitplication loop
> go to cell(1)
[>+>+<<-] move cell(1) to cell(2) and cell(3)
>> go to cell(3)
[<<+>>-] move cell(3) back to cell(1)
<<<- decrement cell(0)
]
>[-]< set cell(1) to 0 so that it can be used as counter
++++[>++++[>+++<-]<-] adding 48 / 0x30 / '0' to cell(2)
>>. print result
请注意,它仍然只适用于小于 10 的结果
一个非常好的资源是 the Esolangs page on Brainfuck algorithms。在那里,乘法定义为:
temp0[-]
temp1[-]
x[temp1+x-]
temp1[
y[x+temp0+y-]temp0[y+temp0-]
temp1-]
因此,如果磁带看起来大致像 temp0 temp1 x y
,指针位于 temp0
,则生成的替换将是:
[-]
>[-]
>[<+>-]
<[
>>[<+<<+>>>-]<<<[>>>+<<<-]
>-]
但是,您的主要问题出现在调用.
输出时。您的方法仅在输出个位数时有效。要将单元格的内容输出为数字,可以使用以下代码:
>++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-
<+>]>+>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++++++
<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]
(The original post containing this algorithm.)