Brainfuck的数字总和

ParaH2

我想知道是否有可能仅用代码开头的数字k计算出Brainfuck中1 + 2 + 3 + ... + k的总和?

例如,可以像这样执行1 + 2 + 3:

+++> (在这里,代码创建一个2,将其与三个相加,然后创建一个并添加)

因为我可以这样做:+++>++>+[<<+>>-]<[<+>-]<但是如果k = 10000怎么办?

英戈·布尔克(IngoBürk)

这是一个简单的版本。假设我们的名字前三个单元格ytemp0x然后,我们可以在递减循环中简单地使用算法:

+++           # Input number (in fisrt cell)
[             # loop while first cell is not 0
 >[-]         # if second cell is null
 <[>>+<+<-]   # copy first cell in second and third and decrease it to 0
 >[<+>-]      # move second cell in first cell
 <-           # decrement input
]             # go to begin of while loop
>>            # the current cell now has the result

请注意,k = 22由于8位限制,这只能工作要输出数字或处理更大的数字,您将需要付出额外的努力。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章