February 27th, 2019 at 6:07 PM
Recursion, as cool of a concept as it is, is honestly pretty useless for fib unless you're only going after small terms. So yeah, 5-25. Anything more risks a complete stack overflow.
Here's a recursive example of what the call stack will look like:
Returning whatever value.
Now, iteratively:
There are probably errors in this, it's just pseudocode so scoping is probably f*** and I probably don't need to use 32bit integers for small terms.
But the call-stack will look more like:
There's more intensive computation in the one function, but there's no need for a massive call-stack, which in turn makes it run quite a bit quicker.
Recursion is a pretty solution, but that's about it.
Here's a recursive example of what the call stack will look like:
Code:
call fib(20)
call fib(19)
call fib(18)
call fib(17)
...
return 0, 1
}
}
}
}
Now, iteratively:
Code:
uint_32 a, 0;
uint_32 b, 1;
uint_32 c;
uint_32 fib(int x){
for(int i = 0; i < x; i++){
c = a+b
a = b;
b = c;
}
return c;
}
fib(20)
But the call-stack will look more like:
Code:
call fib(20)
loop
}
There's more intensive computation in the one function, but there's no need for a massive call-stack, which in turn makes it run quite a bit quicker.
Recursion is a pretty solution, but that's about it.