English 中文(简体)
斐波那契数列代码比赛
原标题:
  • 时间:2008-10-24 08:49:38
  •  标签:
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.

用最少量的字符生成斐波那契数列。除了只能使用 f 运算符并打印斐波那契数列以外,任何语言都可以。

起始点:25 14个字符Haskell编写:

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

f=0:scanl(+)1f
最佳回答

RePeNt, 9, 8 chars

1↓[2?+1]

或者带有打印的10个字符

1↓[2?+↓£1]

使用以下命令运行:

RePeNt "1↓[2?+1]"

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.

答案在自行构建起来的堆栈上。

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

它永远不会终止(它具有等效于C#/JAVA do { } while(true)循环的形式),因为序列永远不会终止,但可以编写终止方案如下:

N_1↓nI{2?+}

哪个是12个字符。

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

问题回答

18个英文字母。

斐波那契数列

好的,我失败了。 :)

13个字符的Golfscript:

2,~{..p@+.}do

更新以解释脚本的操作:

  1. 2, makes an array of [0 1]
  2. ~ puts that array on the stack
  3. So, at the time we run the do, we start the stack off with 0 1 (1 at top of stack)

do 循环:

  1. Each . duplicates the top item of the stack; here, we do this twice (leaving us with 0 1 1 1 on initial run)
  2. p prints the topmost value (leaving us with 0 1 1)
  3. @ rotates the top 3 items in the stack, so that the third-topmost is at the top (1 1 0)
  4. + adds the top 2 items in the stack (leaving 1 1)
  5. . duplicates the top value, so that the do loop can check its truthiness (to determine whether to continue)

通过进行几次心理追踪,就足以告诉你,这做到了所需的加法以生成斐波那契数列的值。

由于GolfScript具有大整数,因此永远不会发生整数溢出,因此在 do 循环结束时,堆栈顶部的值永远不会为0。因此,脚本将永远运行。

Language: C++ Compiler Errors
Characters: 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; }

Perl 6 - 六语言

sub f{1,1...{$^a+$^b}}

x86 (C-callable) realmode, 14 bytes.
Input is  n  on stack, returns  Fn  in AX.

59 31 C0 E3 08 89 C3 40 93 01 D8 E2 FB C3

"Brainfuck"的中文翻译为:"脑瘤语"。

+.>+.[<[>+>+<<-]>.[<+>-]>[<+>-]<]

抵制DC:22个字符

1[pdd5**v1++2/lxx]dsxx

使用以下任意一种方式调用:

dc -e 1[pdd5**v1++2/lxx]dsxx 

或者

echo  1[pdd5**v1++2/lxx]dsxx  | dc

注意:这不是我的作品,是从perlmonks盗用的。

J,一个非递归函数的27个字符。

f=:3 : {:}.@(,+/)^:y(0 1x) 

+/ sums over a list.
(,+/) appends the sum of a list to its tail.
}.@(,+/) sums a list, appends an element to its tail, and drops the first element.
}.@(,+/)^:y iterates the above function y times.
}.@(,+/)^:y(0 1x) applies the above function to the list (0,1) (the x makes it an integer).
{:}.@(,+/)^:y(0 1x) takes the last element of the output list of the above.
f=:3 : {:}.@(,+/)^:y(0 1x) defines f to be a function on one variable y.

记录一下:

  • Lua (66 chars): function f(n)if n<2 then return n else return f(n-1)+f(n-2)end end
  • JavaScript (41 chars): function f(n){return n<2?n:f(n-1)+f(n-2)}
  • Java (41 chars): int f(int n){return n<2?n:f(n-1)+f(n-2);}

我不太擅长超紧凑的语言... :-P

克里斯是对的,我只是用了简单的递归算法。实际上,在Lua中,线性算法甚至更短(多重赋值的功劳)!JavaScript不太幸运,而Java更糟糕,需要声明变量…

  • Lua (60 chars): function f(n)a=1;b=0;for i=1,n do a,b=b,a+b end return b end
  • JavaScript (60 chars): function f(n){a=1;b=i=0;for(;i++<n;){x=a+b;a=b;b=x}return b}
  • Java (71 chars): int f(int n){int a=1,b=0,i=0;for(;i++<n;){int x=a+b;a=b;b=x;}return b;}

I would write Lua s code with local a,b=1,0 but it is longer, so let s pollute _G! ;-) Idem for JS.

为了完整起见,这里给出终端递归版本。使用尾调用的Lua版本与线性版本一样快(但是69个字符,是最长的!)-需要用三个参数n,1,0调用它们。

  • Lua (69 char, longer!): function f(n,a,b)if n<1 then return b else return f(n-1,b,a+b)end end
  • JavaScript (44 chars): function f(n,a,b){return n<1?b:f(n-1,b,a+b)}
  • Java (52 chars): int f(int n,int a,int b){return n<1?b:f(n-1,b,a+b);}

Corrected after comments (thanks Sebastian), it wasn t a sequence solution, so here we go with 42 chars (includes the ):

def f(a=0,b=1):
 while 1:yield a;a,b=b,a+b

OLD post below

Python,共38个字符。

f=lambda n:n if n<2 else f(n-1)+f(n-2)

我认为这篇文章不算太短,但非常易读 :P

EDIT: Here is the analytic way (if someone needs to see it in python :-)

f=lambda n:int(.5+(.5+5**.5/2)**n/5**.5)

Windows XP(以及以后的版本)批处理脚本。此批处理函数在给定单个参数-金额时,生成金额+1个斐波那契数并将它们作为字符串(批处理中没有真正的集合)返回到变量%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

以下是完整的脚本,可在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

微软批处理

旧的挑战,但世界必须知道它是可能的:

%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.

Language: dc, Char count: 20

更短的直流解决方案。

dc -e 1df[dsa+plarlbx]dsbx 

F#:

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

44 个字符

这是我最好的计划, 只有45个字符:

(let f((a 0)(b 1))(printf"~a,"b)(f b(+ a b)))

MS Excel:11个字符:

=SUM(A1:A2)

在前两个单元格中键入 1,然后把上面的公式放在 A3 单元格中。将公式复制到电子表格中。

Starts losing accuracy due to floating point rounding on row 74.
Exceeds 10^307 and overflows to a #NUM! error on row 1477.

Generate the Fibonacci sequence. sequence SEQUENCE!

C#

我看到了很多答案实际上并没有生成序列,而是仅仅使用递归给出第*n*个斐波那契数,当循环生成序列时,在较高的*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) ;
}
let rec f l a b =function 0->a::l|1->b::l|n->f (a::l) b (a+b) (n-1) in f [] 1 1;;

80 个字符,但确实能够在线性时间内生成序列。

红宝石

def f(n)n<2?n:f(n-1)+f(n-2)end

@Andrea Ambu 的中文翻译是:@安德里亚·安布

一个迭代的Pythonic fibonacci() 的版本应该看起来像这样:

def fibonacci(a=0, b=1):
    while True:
        yield b
        a, b = b, a+b

Lua - 4 9 字符

function f(n)return n<2 and n or f(n-1)+f(n-2)end

Befunge-93

31 chars

将输出一个从0开始的无限的斐波那契数列表,以制表符分隔(可以通过删除第一行中的9,以缩短为29个字符,但会损失数字之间的空格)。

不幸的是,我尝试过的所有 Befunge-93 解释器似乎在 65k 后溢出,因此输出仅在 46368(即F24)及以下才是正确的。

#v::1p1>01g:.:01p+9,#
 >     ^

已确认可以使用(附上例外)Befunge-93 Javascript解释器 Visual Befunge Applet Full

我很自豪地说,这是完全原创的作品(即我没有从任何人那里复制这段代码),而且它比Rosetta Code目前的Befunge解决方案要短得多。

BrainF**k:脑交**语言

>+++++>+>+<[[>]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[>+>+<<-]>>[<<+>>-]<[<]>-]

那会生成前五个。要生成更多,请将开头的5 +替换为更多:例如:

>++++++++++++++++++++++>+>+<[[>]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[>+>+<<-]>>[<<+>>-]<[<]>-]

不是最短的,但是在发布时是最快的。 :-)

float f(float n) {
    return (pow(1+sqrt(5.0))/2.0),n) - pow(1+sqrt(5.0))/2.0),n)/sqrt(n));
}

C语言中的33个字符

F(n){return n<2?n:F(n-1)+F(n-2);}

Delphi Prism(Delphi for .net)

f:func<int32,int32>:=n->iif(n>1,f(n-1)+f(n-2),n)

我爱你。

之前的Ruby示例没有分号或换行符是无法工作的,因此实际上是32个字符。以下是第一个实际输出序列的示例,而不仅仅是返回指定索引值的示例。

Ruby:
53 chars, including newlines:

def f(n);n<2?1:f(n-1)+f(n-2);end
0.upto 20 {|n|p f n}

或者,如果您希望功能输出可用的数据结构,则为71个字符:

def f(n);n<2?1:f(n-1)+f(n-2);end
def s(n);(0..n).to_a.map {|n| f(n)};end

是否接受命令行参数,70个字符:

def f(n);n<2?1:f(n-1)+f(n-2);end
p (0..$*[0].to_i).to_a.map {|n| f(n)}

PDP-11汇编器(来源:source

    .globl  start
    .text
start:
    mov $0,(sp)
    mov $27,-(sp)
    jsr pc, lambda
print_r1:
    mov $outbyte,r3
div_loop:
    sxt r0
    div $12,r0
    add $60,r1
    movb    r1,-(r3)
    mov r0,r1
    tst r1
    jne div_loop
    mov $1,r0
    sys 4; outtext; 37
    mov $1,r0
    sys 1
lambda:
    mov 2(sp),r1
    cmp $2,r1
    beq gottwo
    bgt gotone
    sxt r0
    div $2,r0
    tst r1
    beq even
odd:
    mov 2(sp),r1
    dec r1
    sxt r0
    div $2,r0
    mov r0,-(sp)
    jsr pc,lambda
    add $2,sp
    mov r0,r3
    mov r1,r2
    mov r3,r4
    mul r2,r4
    mov r5,r1
    mov r3,r4
    add r2,r4
    mul r2,r4
    add r5,r1
    mul r3,r3
    mov r3,r0
    mul r2,r2
    add r3,r0
    rts pc
even:
    mov 2(sp),r1
    sxt r0
    div $2,r0
    dec r0
    mov r0,-(sp)
    jsr pc,lambda
    add $2,sp
    mov r0,r3
    mov r1,r2
    mov r2,r4
    mul r2,r4
    mov r5,r1
    mov r2,r4
    add r3,r4
    mul r4,r4
    add r5,r1
    mov r2,r4
    add r3,r4
    mul r2,r4
    mov r5,r0
    mul r2,r3
    add r3,r0
    rts pc
gotone:
    mov $1,r0
    mov $1,r1
    rts pc
gottwo:
    mov $1,r0
    mov $2,r1
    rts pc

    .data
outtext:
    .byte 62,63,162,144,40,106,151,142,157,156
    .byte 141,143,143,151,40,156,165,155
    .byte 142,145,162,40,151,163,40
    .byte 60,60,60,60,60
outbyte:
    .byte 12




相关问题
热门标签