Skip to content

Instantly share code, notes, and snippets.

@marclundgren
Last active November 23, 2016 03:18
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 marclundgren/ceb14b7c81492356d65c2683b9c2160e to your computer and use it in GitHub Desktop.
Save marclundgren/ceb14b7c81492356d65c2683b9c2160e to your computer and use it in GitHub Desktop.
fibonacci algorithms (recursive and iterative)
function fib(num) {
if (num === 0) {
return 0
}
if (num === 1 || num === 2) {
return 1
}
let a = 1
let b = 1
for (let index = 3; index < num; index++) {
[a,b] = [a + b, a]
}
return a + b
}
function fibRecursive(num) {
if (num === 0) {
return 0
}
if (num === 1 || num === 2) {
return 1
}
return fibRecursive(num - 1) + fibRecursive(num - 2)
}
@marclundgren
Copy link
Author

@marclundgren
Copy link
Author

todo: i can replace the temporary variable with this pattern: https://files.slack.com/files-pri/T04KHD82U-F2TEYNB26/pasted_image_at_2016_10_24_10_31_am.png

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