Skip to content

Instantly share code, notes, and snippets.

@gusrub
Created May 14, 2021 17:54
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 gusrub/2d3eaa2416b858dbfedc51839bf948dd to your computer and use it in GitHub Desktop.
Save gusrub/2d3eaa2416b858dbfedc51839bf948dd to your computer and use it in GitHub Desktop.
// RUN THIS IN A NODE CONSOLE
let callTimes = 0;
function fibonacci(n) {
callTimes++;
switch(n) {
case 0:
return 0;
case 1:
return 1;
default:
return fibonacci(n-1) + fibonacci(n-2);
}
}
for(i = 0; i <= 10; i++) {
callTimes = 0;
console.log("\n");
console.log(`Result of fi for ${i} is: ${fibonacci(i)}`);
console.log(`Function called itself ${callTimes} times for number ${i}`);
}
// END OF CODE
// Some explanation from wikipedia:
// In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence,
// called the Fibonacci sequence, such that each number is the sum of the two
// preceding ones, starting from 0 and 1. That is
// 2
1 = [f(2-1 = 1)] + [f(2-2) = 0]
// 3
2 = [f(3-1) = 2] + [f(3-2) = 1]
// 4
3 = [f(4-1) = 2] + [f(4-2) = 1]
// 5
5 = [f(5-1) = 3] + [f(5-2) = 2]
// 6
8 = [f(6-1) = 5] + [f(6-2) = 3]
// 7
13 = [f(7-1) = 8] + [f(7-2) = 5]
// 8
21 = [f(8-1) = 13] + [f(8-2) = 8]
// 9
34 = [f(9-1) = 21] + [f(9-2) = 13]
// 10
55 = [f(10-1) = 34] + [f(10-2) = 21]
// So the interesting part here is that you'll see a pattern; the resulting
// output of each cycle in the loop calling itself will be the sum of the result
// of the two preceding numbers, for instance for 5 you see that the result is
// 3+3, for the fi of 8, it is 5 + 3, for 13 it is 8 + 5 and so on. So, if you
// see at the output of the snippet and you sum the two last results before it
// you'll get the next one.
// What's more important, and interesting, is that this is recursion so the way
// you can actually figure out how many times the function is called itself
// is by also summing up the amount of times the function was called itself for
// the last two numbers as well +1, why the +1? because the result for the first
// actual version of the function where it yields to the default where it starts
// recursion is the third one (you can see its called more than once, 3 times)
// and its result its 1
// so if you start from there you'll know that:
// 1 + 1 + 1 = 3
// 1 + 3 + 1 = 5
// 3 + 5 + 1 = 9
// 5 + 9 + 1 = 15
// 9 + 15 + 1 = 25
// etc...
@gusrub
Copy link
Author

gusrub commented May 14, 2021

Here are some pictures to help you figure it out:

Screenshot from 2021-05-14 00-21-26

And to properly understand the pattern of recursion or how many times the function is called itself and needs to return to the former call n'th times to yield and finish, remember, sum the number of times the last two iterations needed and then add one up and you'll get the next amount of times the next fi number function will need to call itself

Screenshot from 2021-05-14 10-48-56

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