Cla*_*diu 27 language-agnostic code-golf fibonacci rosetta-stone
以尽可能少的字符生成Fibonacci序列.任何语言都可以,除了您使用一个运算符定义的语言f,它会打印斐波那契数字.
起点:25 14个字符的哈斯克尔:
f=0:1:zipWith(+)f(tail f)
f=0:scanl(+)1f
Run Code Online (Sandbox Code Playgroud)
Chr*_*ung 33
13张高尔夫球手:
2,~{..p@+.}do
Run Code Online (Sandbox Code Playgroud)
更新以解释脚本的操作:
2, 制作一个数组 [0 1]~ 将该数组放在堆栈上do,我们开始堆栈0 1(堆栈顶部的1)该do循环:
.复制堆栈的顶部项目; 在这里,我们做了两次(让我们0 1 1 1在初始运行时)p打印出最高的值(留给我们0 1 1)@旋转堆栈中的前3项,以便第三个顶部位于顶部(1 1 0)+添加堆栈中的前2项(离开1 1).复制顶部值,以便do循环可以检查其真实性(以确定是否继续)在心理上跟踪几个循环将足以告诉您这是生成Fibonacci序列值所需的添加.
由于GolfScript有bignums,因此永远不会有整数溢出,因此do循环结束时堆栈顶部的值永远不会为0.因此,脚本将永远运行.
Cal*_*ers 29
1?[2?+1]
Run Code Online (Sandbox Code Playgroud)
或打印10个字符:
1?[2?+?£1]
Run Code Online (Sandbox Code Playgroud)
运行使用:
RePeNt "1?[2?+1]"
Run Code Online (Sandbox Code Playgroud)
RePeNt是我编写的基于堆栈的玩具语言(并且仍在改进),其中所有运算符/函数/块/循环都使用反向波兰表示法(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.
Run Code Online (Sandbox Code Playgroud)
答案是在堆栈上构建自己,如:
1 1
1 1 2
1 1 2 3
1 1 2 3 5
Run Code Online (Sandbox Code Playgroud)
它永远不会终止(它具有C#/ JAVA do { } while(true)循环的eqivilent ),因为序列永远不会终止,但是可以编写终止解决方案:
N_1?nI{2?+}
Run Code Online (Sandbox Code Playgroud)
这是12个字符.
我想知道是否有人会读到这个:(
Ecl*_*pse 14
语言:C++编译器错误
字符:205
#define t template <int n> struct
#define u template <> struct f
t g { int v[0]; };
t f { enum { v = f<n-1>::v + f<n-2>::v }; g<v> x;};
u<1> { enum { v = 1 }; };
u<0> { enum { v = 0 }; };
int main() { f<10> x; }
Run Code Online (Sandbox Code Playgroud)
I. *_*edy 11
x86(C-callable)realmode,14个字节.
输入在堆栈上为n, 在AX中返回F n.
59 31 C0 E3 08 89 C3 40 93 01 D8 E2 FB C3
Run Code Online (Sandbox Code Playgroud)
Eri*_*sen 11
Brainfuck,33个字符:
+.>+.[<[>+>+<<-]>.[<+>-]>[<+>-]<]
Run Code Online (Sandbox Code Playgroud)
Chr*_*ung 10
带dc的22个字符:
1[pdd5**v1++2/lxx]dsxx
Run Code Online (Sandbox Code Playgroud)
用以下任一方式调用:
dc -e'1[pdd5**v1++2/lxx]dsxx'
要么:
echo '1[pdd5**v1++2/lxx]dsxx' | dc
注意:不是我的工作,从perlmonks偷猎.
J,非递归函数的27个字符:
f=:3 :'{:}.@(,+/)^:y(0 1x)'
Run Code Online (Sandbox Code Playgroud)
+/总结清单.
(,+/)将列表的总和附加到其尾部.
}.@(,+/)对列表求和,在其尾部追加一个元素,并删除第一个元素.
}.@(,+/)^:y迭代上述函数y时间.
}.@(,+/)^:y(0 1x)将上述函数应用于列表(0,1)(x使其成为整数).
{:}.@(,+/)^:y(0 1x)获取上面输出列表的最后一个元素.
f=:3 :'{:}.@(,+/)^:y(0 1x)'定义f为一个变量的函数y.
作为记录:
function f(n)if n<2 then return n else return f(n-1)+f(n-2)end endfunction f(n){return n<2?n:f(n-1)+f(n-2)}int f(int n){return n<2?n:f(n-1)+f(n-2);}我不太熟悉超简洁的语言...... :-P
克里斯是对的,我只是采用了简单的递归算法.实际上,线性的在Lua中甚至更短(多亏了多次分配)!JavaScript不是那么幸运,Java更糟糕,不得不声明变量......
function f(n)a=1;b=0;for i=1,n do a,b=b,a+b end return b endfunction f(n){a=1;b=i=0;for(;i++<n;){x=a+b;a=b;b=x}return b}int f(int n){int a=1,b=0,i=0;for(;i++<n;){int x=a+b;a=b;b=x;}return b;}我会写Lua的代码,local a,b=1,0但它更长,所以让我们污染_G!;-) Idem for JS.
为了完整起见,这里是终端递归版本.Lua的一个,使用尾部调用,和线性调用一样快(但是69个字符,它是最长的!) - 需要用三个参数n,1,0来调用它们.
function f(n,a,b)if n<1 then return b else return f(n-1,b,a+b)end endfunction f(n,a,b){return n<1?b:f(n-1,b,a+b)}int f(int n,int a,int b){return n<1?b:f(n-1,b,a+b);}在评论后更正(感谢塞巴斯蒂安),这不是一个序列解决方案,所以这里我们使用42个字符(包括\n):
def f(a=0,b=1):
while 1:yield a;a,b=b,a+b
Run Code Online (Sandbox Code Playgroud)
Python,38个字符.
f=lambda n:n if n<2 else f(n-1)+f(n-2)
Run Code Online (Sandbox Code Playgroud)
不是那么短,但在我看来最具可读性:P
编辑:这是分析方式(如果有人需要在python中看到它:-)
f=lambda n:int(.5+(.5+5**.5/2)**n/5**.5)
Run Code Online (Sandbox Code Playgroud)
Windows XP(及更高版本)批处理脚本.当给定单个参数时,此批处理函数 - 金额,生成金额+ 1个斐波纳契数并将它们作为字符串返回(BATCH实际上没有集合)变量%r%(369个字符,或347个字符 - 如果我们删除缩进) :
:f
set i=0
set r=1
set n=1
set f=0
:l
if %n% GTR %~1 goto e
set f=%f% %r%
set /A s=%i%+%r%
set i=%r%
set r=%s%
set /A n+=1
goto l
:e
set r=%f%
exit /B 0
Run Code Online (Sandbox Code Playgroud)
这是完整的脚本,可以看到它的运行情况(只需将其复制到CMD或BAT文件并运行它):
@echo off
call :ff 0
call :ff 1
call :ff 2
call :ff 3
call :ff 5
call :ff 10
call :ff 15
call :ff 20
exit /B 0
:ff
call :f "%~1"
echo %~1: %r%
exit /B 0
:f
set i=0
set r=1
set n=1
set f=0
:l
if %n% GTR %~1 goto e
set f=%f% %r%
set /A s=%i%+%r%
set i=%r%
set r=%s%
set /A n+=1
goto l
:e
set r=%f%
exit /B 0
Run Code Online (Sandbox Code Playgroud)
小智 6
Microsoft Batch - 15个字符
旧的挑战,但世界必须知道它是可能的:
%1
%0 %1%2 %1 #
Run Code Online (Sandbox Code Playgroud)
输出是staryr in anary,只计算#个字符.根据主机系统的空间限制,它可能只产生前14个数字左右.
这是我最好的使用方案,有45个字符:
(let f((a 0)(b 1))(printf"~a,"b)(f b(+ a b)))
Run Code Online (Sandbox Code Playgroud)