Skip to content

Instantly share code, notes, and snippets.

@zurfyx
Last active March 29, 2018 18:14
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 zurfyx/47cf03423d2349ab5dbfab7ccc56adc6 to your computer and use it in GitHub Desktop.
Save zurfyx/47cf03423d2349ab5dbfab7ccc56adc6 to your computer and use it in GitHub Desktop.
/node_modules
#!/usr/bin/env node
const _ = require('lodash');
/**
* Recursive version of fibonacci using memoize technique and lodash.
*/
const fibonacci = _.memoize((n) => (
n <= 1 ? Math.max(0, n) : fibonacci(n - 1) + fibonacci(n - 2)
));
if (require.main == module) {
const n = process.argv[2];
if (n === undefined) {
console.error(`${process.argv[0]} ${process.argv[0]} <number>`);
process.exit(1);
}
if (n < 0) {
console.error(`Enter a positive number.`);
process.exit(1);
}
const val = fibonacci(n);
console.info(`Fibonacci digit @ ${n}: ${val}`);
}
{
"name": "ngx-demo",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"lodash": {
"version": "4.17.5",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.5.tgz",
"integrity": "sha512-svL3uiZf1RwhH+cWrfZn3A4+U58wbP0tGVTLQPbjplZxZ8ROD9VLuNgsRniTlLe7OlSqR79RUehXgpBW/s0IQw=="
}
}
}
{
"name": "npx-demo",
"version": "1.0.2",
"description": "",
"main": "index.js",
"bin": "index.js",
"repository": {
"url": "https://gist.github.com/zurfyx/47cf03423d2349ab5dbfab7ccc56adc6"
},
"dependencies": {
"lodash": "^4.17.5"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment