g | x | w | all
Bytes Lang Time Link
038Python240528T161039Zstwq
012Uiua240529T154944Znoodle m
023x86 machine code200829T151717ZFebriyan
003Neim200901T124324ZLiefdeWe
003Japt m190817T231935ZShaggy
045Python 3200901T014457ZMatthew
037JavaScript V8200901T014331ZMatthew
038Python 2200205T231123ZRussell
379x86 machine code190817T183218ZKamila S
003Pyt180201T053743Zqqq
020FALSE180131T143025Z12Me21
028FALSE120207T194142Zuser3725
085Lua120216T015222Zuser3781
028Perl160306T133025ZTon Hosp
nan150730T125521ZDonald H
032dc120531T091920Zdaniero
026Haskell121208T020538ZLambda F
024Q120302T164300Zsinedcm
065Scala120601T091547ZDon Mack
123F#120525T051546ZSmetad A
043Python120525T072551Zboothby
033APL120523T163820Zmarinus
043TIBasic120513T163203ZPhiNotPi
087PHP120502T074517Zkarth
048CoffeeScript120502T053433ZRicardo
035Powershell120121T062028ZSpelling
016GolfScript120124T111452Zhammar
025J120121T194120ZGareth
058C99120121T184403Zhan
nan120119T000004Zlbolla
nan120118T200121ZSamuel D
nan120118T130727Zuser unk
050Perl120118T124842ZToto
nanHere's a oneliner Python. It uses floatingpoint120117T233032ZKeith Ra
078Python120118T031012Zelssar
nan120117T223024ZMr. Llam
nan120117T215423ZKris Har

Python (38 bytes)

a,b=0,1;exec("print(a);a,b=b,a+b;"*31)

the factor of multiplication prints the first N

Edit: First n from stdin (48 bytes)

a,b=0,1;exec("print(a);a,b=b,a+b;"*int(input()))

Uiua, 12 bytes

[β—Œβ—Œβ₯βŠƒ+.:0:1]

Try it online

Outputs in reverse order.

x86 machine code - 23 bytes

08048060 <_start>:
 8048060:       6a 01                   push   0x1
 8048062:       59                      pop    ecx
 8048063:       6a 02                   push   0x2
 8048065:       5f                      pop    edi

08048066 <_start.loop>:
 8048066:       89 da                   mov    edx,ebx
 8048068:       89 c8                   mov    eax,ecx
 804806a:       01 d0                   add    eax,edx
 804806c:       89 c6                   mov    esi,eax
 804806e:       89 c3                   mov    ebx,eax
 8048070:       89 f0                   mov    eax,esi
 8048072:       89 c1                   mov    ecx,eax
 8048074:       47                      inc    edi
 8048075:       eb ef                   jmp    8048066 <_start.loop>

You can assembling and debug it using GDB, in debugger set a breakpoint at _start label (NASM style) and type s (mean single-step) command, see the output in register eax.

Output : 0 1 1 2 ...

Neim, 3 bytes

f   # Fibonacci list
 I  # First line of input
  𝕖 # Get first b elements of a

Try it online!

Japt -m, 4 3 bytes

Starts from 0.

MgU

Try it

Python 3, 45 bytes

f=lambda n,i=0,j=1:n and[i,*f(n-1,j,i+j)]or[]

Try it online!

A similar implementation to my JavaScript answer.

JavaScript (V8), 37 bytes

f=(n,i=0,j=1)=>--n?i+' '+f(n,j,i+j):i

Try it online!

Python 2, 38 Bytes

An improvement on a previously posted solution:

a=b=1
exec'print a;a,b=b,a+b;'*input()

This uses exec and string multiplication to avoid loops.

Python 3, 46 Bytes

Not quite as efficient in Python 3:

a=b=1
exec('print(a);a,b=b,a+b;'*int(input()))

x86 machine code - 379 bytes

The version with ELF headers scoring 484 bytes:

00000000: 7f45 4c46 0101 0100 0000 0000 0000 0000  .ELF............
00000010: 0200 0300 0100 0000 c080 0408 3400 0000  ............4...
00000020: 0000 0000 0000 0000 3400 2000 0200 2800  ........4. ...(.
00000030: 0000 0000 0100 0000 0000 0000 0080 0408  ................
00000040: 0000 0000 e401 0000 0010 0000 0500 0000  ................
00000050: 0010 0000 0100 0000 0000 0000 0090 0408  ................
00000060: 0000 0000 0000 0000 0000 1000 0600 0000  ................
00000070: 0010 0000 0000 0000 0000 0000 0000 0000  ................
00000080: 51b9 0090 0408 8801 31c0 ba01 0000 00eb  Q.......1.......
00000090: 0351 89c1 31c0 89c3 43b0 04cd 8031 c099  .Q..1...C....1..
000000a0: 4259 c300 0000 0000 0000 0000 0000 0000  BY..............
000000b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000c0: 31c0 9942 b903 9004 08c6 4101 0ac6 4102  1..B......A...A.
000000d0: 01c6 4103 013a 7103 0f84 ff00 0000 3a71  ..A..:q.......:q
000000e0: 0374 2680 4103 050f b641 036b c008 0041  .t&.A....A.k...A
000000f0: 048a 4104 e887 ffff ff80 6904 30c6 4103  ..A.......i.0.A.
00000100: 0183 e903 3a71 0375 da8a 4104 e86f ffff  ....:q.u..A..o..
00000110: ff3a 7106 0f84 ba00 0000 0fb6 4105 8841  .:q.........A..A
00000120: 060f b641 0788 4105 0fb6 4107 0041 06c6  ...A..A...A..A..
00000130: 4107 003a 7106 0f84 8800 0000 c641 0701  A..:q........A..
00000140: fe49 063a 7106 0f84 7800 0000 c641 0702  .I.:q...x....A..
00000150: fe49 063a 7106 0f84 6800 0000 c641 0703  .I.:q...h....A..
00000160: fe49 063a 7106 0f84 5800 0000 c641 0704  .I.:q...X....A..
00000170: fe49 063a 7106 744c c641 0705 fe49 063a  .I.:q.tL.A...I.:
00000180: 7106 7440 c641 0706 fe49 063a 7106 7434  q.t@.A...I.:q.t4
00000190: c641 0707 fe49 063a 7106 7428 c641 0708  .A...I.:q.t(.A..
000001a0: fe49 063a 7106 741c c641 0709 fe49 063a  .I.:q.t..A...I.:
000001b0: 7106 7410 fe41 08fe 4109 fe49 060f b641  q.t..A..A..I...A
000001c0: 0688 4107 c641 0601 83c1 033a 7106 0f85  ..A..A.....:q...
000001d0: 46ff ffff 3a71 030f 8501 ffff ffb3 0031  F...:q.........1
000001e0: c040 cd80                                .@..

Headerless version (that is the one to be graded):

00000000: 67c6 4101 0a67 c641 0201 67c6 4103 0167  g.A..g.A..g.A..g
00000010: 3a71 030f 842a 0167 3a71 0374 2e67 8041  :q...*.g:q.t.g.A
00000020: 0305 6667 0fb6 4103 666b c008 6700 4104  ..fg..A.fk..g.A.
00000030: 678a 4104 e80d 0167 8069 0430 67c6 4103  g.A....g.i.0g.A.
00000040: 0166 83e9 0367 3a71 0375 d267 8a41 04e8  .f...g:q.u.g.A..
00000050: f200 673a 7106 0f84 df00 6667 0fb6 4105  ..g:q.....fg..A.
00000060: 6788 4106 6667 0fb6 4107 6788 4105 6667  g.A.fg..A.g.A.fg
00000070: 0fb6 4107 6700 4106 67c6 4107 0067 3a71  ..A.g.A.g.A..g:q
00000080: 060f 84a3 0067 c641 0701 67fe 4906 673a  .....g.A..g.I.g:
00000090: 7106 0f84 9200 67c6 4107 0267 fe49 0667  q.....g.A..g.I.g
000000a0: 3a71 060f 8481 0067 c641 0703 67fe 4906  :q.....g.A..g.I.
000000b0: 673a 7106 0f84 7000 67c6 4107 0467 fe49  g:q...p.g.A..g.I
000000c0: 0667 3a71 0674 6167 c641 0705 67fe 4906  .g:q.tag.A..g.I.
000000d0: 673a 7106 7452 67c6 4107 0667 fe49 0667  g:q.tRg.A..g.I.g
000000e0: 3a71 0674 4367 c641 0707 67fe 4906 673a  :q.tCg.A..g.I.g:
000000f0: 7106 7434 67c6 4107 0867 fe49 0667 3a71  q.t4g.A..g.I.g:q
00000100: 0674 2567 c641 0709 67fe 4906 673a 7106  .t%g.A..g.I.g:q.
00000110: 7416 67fe 4108 67fe 4109 67fe 4906 6667  t.g.A.g.A.g.I.fg
00000120: 0fb6 4106 6788 4107 67c6 4106 0166 83c1  ..A.g.A.g.A..f..
00000130: 0367 3a71 060f 8521 ff67 3a71 030f 85d6  .g:q...!.g:q....
00000140: fe00 0000 6651 66b9 7801 0000 6788 0166  ....fQf.x...g..f
00000150: 31c0 66ba 0100 0000 eb05 6651 6689 c166  1.f.......fQf..f
00000160: 31c0 6689 c366 43b0 04cd 8066 31c0 6699  1.f..fC....f1.f.
00000170: 6642 6659 c300 0000 0000 00              fBfY.......

Calculates (possibly) \$ \infty \$ fibonacci numbers.

Pyt, 3 bytes

Ε™β»αΈž

Try it online!

Ε™ creates an array [1, 2, 3, ..., x]
⁻ decrements every item once (as Ḟ is 0 indexed)
Ḟ for every item in x converts it to it's fibonacci equivalent

FALSE, 20 characters

^1@[1-$][@2ΓΈ+$.\9,]#

Input should be on the stack before running this.

FALSE, 28 bytes

0 1- 1 10[$][@@$@+$." "@1-]#

Lua, 85 bytes

I am learning Lua so I would like to add this language to the pool.

function f(x)
    return (x<3) and 1 or f(x-1)+f(x-2)
end
for i=1,io.read() do
    print(f(i))
end

and the whole thing took 85 characters, with the parameter as a command line argument. Another good point is that is easy to read.

Perl, 29 28 bytes

perl -E'say$b+=$;=$b-$;for-pop..--$;' 8
1
1
2
3
5
8
13
21

Explanation

This is based on the classic $b += $a = $b-$a recurrence which works as follows:

The problem here is the initialization. The classical way is $b += $a = $b-$a || 1 but then the sequence goes 1 2 3 5 ...

By extending the fibonacci sequence to the left:

... 5 -3 2 -1 1 0 1 1 2 3 5 ...

you see that the proper starting point is $a = -1 and $b = 0. Initializing $a can be combined with setting up the loop

Finally replace $a by $; to get rid of the space before the for

Python(55)

a,b=0,1

for i in range(int(input())):a,b=b,a+b;print(b)

dc, 32 characters:

This will actually always show the two first 1's, so the function only work as expected for N >= 2.

?2-sn1df[dsa+plarln1-dsn0<q]dsqx

C, 75 characters:

Not as cool as the accepted answer, but shorter and way faster:

main(n,t,j,i){j=0,i=scanf("%d",&n);while(n--)t=i,i=j,printf("%d\n",j+=t);}
Extra:

CL, 64 characters:

One of my most used bookmarks this semester has an interesting example which is shorter than many some of the other ones here, and it's just a straight-forward invocation of the loop macro -- basically just one statement! Stripped it for all the whitespace I could:

(loop repeat n for x = 0 then y and y = 1 then(+ x y)collect y)

Quite short, and nice and readable! To read input, n (including surrounding whitespaces) can be replaced with (read), adding 3 characters.

Haskell (26)

Surprisingly, this is only one character longer than the J solution.

f=(`take`s)
s=0:scanl(+)1s

I shave off a few characters by:

  1. Using take as a binary operator;
  2. Using scanl instead of the verbose zipWith.

Q 24

f:{{x,sum -2#x}/[x;0 1]}

First n fibonacci numbers

Scala, 65 characters

(Seq(1,0)/:(3 to 9)){(s,_)=>s.take(2).sum+:s}.sorted map println

This prints, for example, the first 9 Fibonacci numbers. For a more useable version taking the sequence length from console input, 70 characters are required:

(Seq(1,0)/:(3 to readInt)){(s,_)=>s.take(2).sum+:s}.sorted map println

Beware the use of a Range limits this to Int values.

F#, 123

let f n = Seq.unfold(fun (i,j)->Some(i,(j,i+j)))(0,1)|>Seq.take n
f 5|>Seq.iter(fun x->printfn "%i" x)

Python, 43 chars

Here are three fundamentally different one-liners that don't use Binet's formula.

f=lambda n:reduce(lambda(r,a,b),c:(r+[b],a+b,a),'.'*n,([],1,0))[0]
f=lambda n:map(lambda x:x.append(x[-1]+x[-2])or x,[[0,1]]*n)[0]
def f(n):a=0;b=1;exec'print a;a,b=b,a+b;'*n

I've never abused reduce so badly.

APL (33)

{⍎'βŽ•','←0,1',⍨'←A,+/Β―2↑A'⍴⍨9×⍡-2}

Usage:

   {⍎'βŽ•','←0,1',⍨'←A,+/Β―2↑A'⍴⍨9×⍡-2}7
0 1 1 2 3 5 8

TI-Basic, 43 characters

:1β†’Y:0β†’X
:For(N,1,N
:Disp X
:Y→Z
:X+Y→Y
:Z→X
:End

This code can be directly inserted into the main program, or made into a separate program that is referenced by the first.

PHP, 87

function f($n,$a=array(0,1)){echo' '.$a[0];$n>0?f(--$n,array($a[1],array_sum($a))):'';}

Uses array_sum and recursive function to generate series.

Eg:

 $ php5 fibo.php 9
 0 1 1 2 3 5 8 13 21 34 

CoffeeScript, 48

f=(n,i=1,j=1)->(console.log i;f n-1,j,i+j)if n>0

65 in js:

function f(n,i,j){if(n>0)console.log(i),f(n-1,(j=j||1),(i||1)+j)}

Powershell - 35 characters

Powershell accepts pipeline input, so I'm of the belief that the n | in n | <mycode> shouldn't be against my count, but instead is just a part of initiating a "function" in the language.

The first solution assumes we start at 0:

%{for($2=1;$_--){($2=($1+=$2)-$2)}}

The second solution assumes we can start at 1:

%{for($2=1;$_--){($1=($2+=$1)-$1)}}

Example invocation: 5 | %{for($2=1;$_--){($1=($2+=$1)-$1)}}

Yields:

1
1
2
3
5

Interestingly, attempts to avoid the overhead of the for() loop resulted in the same character count: %{$2=1;iex('($1=($2+=$1)-$1);'*$_)}.

GolfScript, 16 characters

~0 1@{.2$+}*;;]`

Example output:

$ ruby golfscript.rb ~/Code/golf/fib.gs <<< "12"
[0 1 1 2 3 5 8 13 21 34 55 89]

J, 25 characters

I realise that J solutions are probably not what you're after, but here's one anyway. :-)

0 1(],+/&(_2&{.))@[&0~2-~

Usage:

    0 1(],+/&(_2&{.))@[&0~2-~ 6
0 1 1 2 3 5
    0 1(],+/&(_2&{.))@[&0~2-~ 10
0 1 1 2 3 5 8 13 21 34

How it works:

Starting from the right (because J programs are read from right to left),

2-~ 6 The ~ operator reverses the argument to the verb so this is the same as 6-2

Ignoring the section in brackets for now, 0 1(...)@[&0~ xtakes the verb in the brackets and executes it x times using the list 0 1 as its input - ~ again reverses the arguments here, giving x (...)@[&0 ] 0 1, meaning I can keep the input at the end of the function.

Within the brackets is a fork ],+/&(_2&{.) which is made up of three verbs - ], , and +/&(_2&{.).

A fork takes three verbs a b c and uses them like this: (x a y) b (x c y) where x and y are the arguments to the fork. The , is the centre verb in this fork and joins the results of x ] y and x +/&(_2&{.) y together.

] returns the left argument unaltered so x ] y evaluates to x.

+/&(_2&{.) takes the last two items from the given list (_2&{.) - in this case 0 1 - and then adds them together +/ (the &s just act as glue).

Once the verb has operated once the result is fed back in for the next run, generating the sequence.

C99, 58 characters

The following function fills an array of integers with the first n values from the Fibonacci sequence starting with 0.

void f(int*a,int n){for(int p=0,q=1;n--;q+=*a++)*a=p,p=q;}

Test harness, taking n as a command line argument:

#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
     int n = (argc > 1) ? atoi(argv[1]) : 1;
     int a[n];
     f(a, n);
     for (int i = 0; i < n; ++i)
          printf("%d\n", a[i]);
}

Haskell

fib n = take n f
f = 0:1:zipWith (+) f (tail f)

Proof that it works.

Scheme

This is optimized using tail-recursion:

(define (fib n)
  (let fib ([n n] [a 0] [b 1])
    (if (zero? n) (list a)
        (cons aΒ (fib (- n 1) b (+ a b))))))

Scala 71:

def f(c:Int,a:Int=0,b:Int=1):Unit={println(a);if(c>0)f(c-1,b,a+b)};f(9)

prints

0
1
1
2
3
5
8
13
21
34

Perl, 50 characters

sub f{($a,$b,$c)=@_;$c--&&say($a)&&f($b,$a+$b,$c)}

Here's a one-liner Python. It uses floating-point, so there may be some n for which it is no longer accurate.

F=lambda n:' '.join('%d'%(((1+5**.5)/2)**i/5**.5+.5)for i in range(n))

F(n) returns a string containing the first n Fibonacci numbers separated by spaces.

Python (78 chars)

I used Binet's formula to calculate the fibonacci numbers -

[(1+sqrt(5))^n-(1-sqrt(5)^n]/[(2^n)sqrt(5)]

It's not as small some of the other answers here, but boy it's fast

n=input()
i=1
x=5**0.5
while i<=n:
    print ((1+x)**i-(1-x)**i)/((2**i)*x)
    i+=1

C

Didn't bother counting, but here's a fun example:

f(n){return n<4?1:f(--n)+f(--n);}
main(a,b){for(scanf("%d",&b);a++<=b;printf("%d ",f(a)));}

Proof it works.


I'm quite proud of this: I got bored, so I rearranged my code (with a few small additions) to make it where each line represents a value in the Fibonacci sequence.

                         #                                // 1
                         f                                // 1
                         //                               // 2
                        (n)                               // 3
                       {/**/                              // 5
                      return n                            // 8
                    <2 ? 1:f(--n)                         // 13
                +f(--n); } main(a, b)                     // 21
          {a = 0, b = 0;scanf("%d",&b); for(              // 34
;a < b; a+=1) { int res = f(a); printf("%d ", res); } }   // 55

Proof it works.

I can give you a two line Python solution. This will return them as a list.

f = lambda n: 1 if n < 2 else f(n-1) + f(n-2)
g = lambda m: map(f, range(0,m))

print g(5)

You could have it print them out by adding another map to make them strings and then adding a join, but that just seems unnecessary to me.

Unfortunately I don't know how to put a recursive lambda into map, so I'm stuck at two lines.