Skip to content

Instantly share code, notes, and snippets.

@CodeGolfScotland
Last active January 25, 2017 09:29
Show Gist options
  • Save CodeGolfScotland/8cc3074fb8ec974fc344a49d821e466d to your computer and use it in GitHub Desktop.
Save CodeGolfScotland/8cc3074fb8ec974fc344a49d821e466d to your computer and use it in GitHub Desktop.

November 2016 - The Fibonacci Sequence

Task

Return the first N items of the Fibonacci sequence.

The input will be a whole number greater than or equal to zero.

The output should be iterable.

Examples:

(0) => [0]
(5) => [0, 1, 1, 2, 3, 5]
(9) => [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

*^You can assume that you will always recieved valid input

About Code Golf Scotland

@joaquinferrero
Copy link

joaquinferrero commented Nov 1, 2016

Language: Perl 6
Length: 29 28 26
Solution:

sub fi{(0,1,*+*…∞)[0..$^a]}

Demo:

perl6 -e 'for 0, 5, 9 { fi($_).join("-").say }; sub fi{(0,1,*+*…∞)[0..$^a]}'
0
0-1-1-2-3-5
0-1-1-2-3-5-8-13-21-34

@nevstokes
Copy link

nevstokes commented Nov 1, 2016

Language: PHP (without witchcraft)

Length: 129

Solution:

function f($n){return array_reduce(range(0,$n),function($c,$i){$c[]=$i>1?array_sum(array_slice($c,-2)):count($c);return$c;},[]);}

Copy link

ghost commented Nov 1, 2016

Language: Elixir
Length: 91
Solution:

def f(0),do: 0
def f(1),do: 1
def f(n),do: f(n-1)+f(n-2)
def l(n),do: Enum.map(0..n,&f(&1))

@billythekid
Copy link

billythekid commented Nov 1, 2016

Language: PHP
Length: 96
Solution:

function
x($n){return
array_map(function($n){return
round(((5**.5+1)/2)**$n/5**.5);},range(0,$n));}

ciaran's wizardry can be shortened to 66 with some whitespace and trailing semicolon removal (PHP7.1):

function
f($n,$a=0,$b=1){while($n--){yield$a;[$a,$b]=[$b,$a+$b];}}

//usage print_r(iterator_to_array(f(9))) etc.

@CraigMorton
Copy link

CraigMorton commented Nov 2, 2016

This solution feels really messy, but anything more elegant ended up a lot longer.

Language: Javascript
Length: 65
Solution:

f=n=>{a=[0,1];for(i=1;i<n;i++){a[i+1]=a[i]+a[i-1]}return n?a:[0]}
// wrapped in callable function f
f(0) // [ 0 ]
f(5) // [ 0, 1, 1, 2, 3, 5 ]

Expanded solution:

let fibonacci = (integer) => {
  let array = [0, 1]
  for (let i = 1; i < integer; i++) {
    array[i+1] = array[i] + array[i-1]
  }
  return (integer != 0) ? array : [0]
}

Copy link

ghost commented Nov 2, 2016

Trying a recursive solution for javascript. Wonder if I can do something like [1..n] to generate an array?

Language: Javascript
Length: 69
Solution:

f=n=>n==0?0:n==1?1:f(n-1)+f(n-2)
s=x=>[...Array(x+1)].map((_,i)=>f(i))

@unfunco
Copy link

unfunco commented Nov 10, 2016

Language: PHP
Length: 60
Solution: (Work in progress: as it misses F0 and F1, and becomes inaccurate quite quickly due to the low-precision, you could replace 0.618 with something like ((1 + sqrt(5)) / 2) - 1) for slightly better accuracy.)

function f($n){yield$f=1;for(;$n--;)yield$f=round($f/.618);}

Language: Ruby
Length: 44
Solution: (Another work in progress, this one is more accurate than the PHP version)

s=[0];n=k=1;1.upto($*[0].to_i){s<<n;k=n+n=k}

@davyboyhayes
Copy link

davyboyhayes commented Nov 11, 2016

Language: Javascript ES6
Length: 70
Solution: (Produces a full array output as expected, and values of n under 0 produce an empty array.)

f=(n)=>n<0?[]:(a=f(n-1),n==0?[0]:n==1?[0,1]:a.concat([a[n-1]+a[n-2]]))

@davyboyhayes
Copy link

davyboyhayes commented Nov 21, 2016

Language: Javascript ES6
Length: 62
Solution: Further optimisation

f=(n)=>n<2?[0,1].splice(0,n+1):(a=f(n-1),a[n]=a[n-1]+a[n-2],a)

@davyboyhayes
Copy link

davyboyhayes commented Nov 21, 2016

Language: Javascript ES6
Length: 59
Solution: Further optimisation - Correct for all values of n>=0. (Behaves as previous example for all negative values of n except for n=-2, but this annoyance is not required by the problem)

f=n=>n<2?[0,1].slice(0,n+1):(a=f(n-1),a[n]=a[n-1]+a[n-2],a)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment