Skip to content

Instantly share code, notes, and snippets.

@doug-numetric
Created July 25, 2017 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doug-numetric/97d874d74a2135f2a3b0dd3b73d99947 to your computer and use it in GitHub Desktop.
Save doug-numetric/97d874d74a2135f2a3b0dd3b73d99947 to your computer and use it in GitHub Desktop.
Efficient fibonacci calculator without loops or recursion
const { reduce, range, map } = require('lodash/fp')
const fibinacci =
num => reduce(
acc => [
acc[0] + acc[1],
acc[0]
],
[1,0],
range(0, num)
)[0];
console.log(map(e => fibinacci(e), [1,2,3,4,5,6,7,8,9]));
// [ 1, 2, 3, 5, 8, 13, 21, 34, 55 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment