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

@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