Fibonacci Code Golf

2019-01-31 07:40发布

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.

Starting point: 25 14 characters in Haskell:

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

f=0:scanl(+)1f

30条回答
Melony?
2楼-- · 2019-01-31 08:17

Ruby (30 characters):

def f(n)n<2?n:f(n-1)+f(n-2)end
查看更多
乱世女痞
3楼-- · 2019-01-31 08:18

Microsoft Batch - 15 characters

Old challenge, but the world must know it is possible:

%1
%0 %1%2 %1 #

Output is to stderr in unary, counting only the # characters. Depending on the host system's space restrictions, it may produce only the first 14 numbers or so.

查看更多
疯言疯语
4楼-- · 2019-01-31 08:21

F#:

(0,1)|>Seq.unfold(fun(a,b)->Some(a,(b,a+b)))

44 Chars

查看更多
孤傲高冷的网名
5楼-- · 2019-01-31 08:22

C#

I'm seeing a lot of answers that don't actually generate the sequence, but instead give you only the fibonacci number at position *n using recursion, which when looped to generate the sequence gets increasingly slower at higher values of n.

using System;
static void Main()
{
  var x = Math.Sqrt(5);
  for (int n = 0; n < 10; n++)
    Console.WriteLine((Math.Pow((1 + x) / 2, n) - Math.Pow((1 - x) / 2, n)) / p) ;
}
查看更多
ら.Afraid
6楼-- · 2019-01-31 08:22

33 characters in C:

F(n){return n<2?n:F(n-1)+F(n-2);}
查看更多
你好瞎i
7楼-- · 2019-01-31 08:23

Perl 6 - 22 characters:

sub f{1,1...{$^a+$^b}}
查看更多
登录 后发表回答