斐波那契密码高尔夫 [英] Fibonacci Code Golf

查看:94
本文介绍了斐波那契密码高尔夫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以尽可能少的字符数生成斐波那契数列.可以使用任何一种语言,但您使用一种运算符f定义的一种语言除外,该语言将打印斐波那契数字.

Generate the Fibonacci sequence in the fewest amount of characters possible. Any language is OK, except for one that you define with one operator, f, which prints the Fibonacci numbers.

起点: Haskell 中的 25 14个字符:

Starting point: 25 14 characters in Haskell:

f=0:1:zipWith(+)f(tail f)

f=0:scanl(+)1f

推荐答案

RePeNt, 9 ,8个字符

RePeNt, 9, 8 chars

1↓[2?+1]

打印10个字符:

1↓[2?+↓£1]

运行方式:

RePeNt "1↓[2?+1]"

RePeNt是我编写(并且还在不断改进)的基于堆栈的玩具语言,其中所有运算符/功能/块/循环都使用反向波兰表示法(RPN).

RePeNt is a stack based toy language I wrote (and am still improving) in which all operators/functions/blocks/loops use Reverse Polish Notation (RPN).

Command      Explanation                                              Stack
-------      -----------                                              -----

1            Push a 1 onto the stack                                  1
↓            Push last stack value                                    1 1
[            Start a do-while loop                                    1 1
2?           Push a two, then pop the 2 and copy the last 2 stack     1 1 1 1
             items onto the stack
+            Add on the stack                                         1 1 2
↓£           Push last stack value then print it                      1 1 2
1            Push a 1 onto the stack                                  1 1 2 1
]            Pop value (1 in this case), if it is a 0 exit the loop   1 1 2
             otherwise go back to the loop start.

答案在堆栈上,堆栈本身会像这样构建:

The answer is on the stack which builds itself up like:

1 1
1 1 2
1 1 2 3
1 1 2 3 5

它永远不会终止(它具有C#/JAVA do { } while(true)循环的效果),因为该序列永远不会终止,但是可以这样编写终止解决方案:

It never terminates (it has the eqivilent of a C#/JAVA do { } while(true) loop) because the sequence will never terminate, but a terminating solution can be written thus:

N_1↓nI{2?+}

这是12个字符.

我想知道是否有人会读到这个:(

I wonder if anyone will ever read this :(

这篇关于斐波那契密码高尔夫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆