alr this thing is totally made up by me so it could be right or could be wrong
but in any case at least 1 bitcoin have been equal to more than 1 kilogram of cheese
the idea is basicly instead of doing recursion with the old fasioned way where
you just call the function again or call another function that calls this function
you basicly do a forloop simulating this function call
why ?
because when you call a function your computer saves some data about the point
where you called that function so that
when you call return => it knows where should it return to
got it ?
when you call the function inside it self it just tells the computer
hey we should come back to line 15 after we are done, store that!
hey we should come back to line 15 after we are done, store that!
hey we should come back to line 15 after we are done, store that!
hey we should come back to line 15 after we are done, store that!
hey we should come back to line 15 after we are done, store that!
hey we should come back to line 15 after we are done, store that!
hey we should come back to line 15 after we are done, store that!
hey we should come back to line 15 after we are done, store that!
hey we should come back to line 15 after we are done, store that!
hey we should come back to line 15 after we are done, store that!
hey we should come back to line 15 after we are done, store that!
hey we should come back to line 15 after we are done, store that!
and your ram will have something like :
15
15
15
15
15
15
15
15
15
15
15
15
and when it finshs it just goes
oh hey i should go back to line 15, remove that and go to line 15
oh hey i should go back to line 15, remove that and go to line 15
oh hey i should go back to line 15, remove that and go to line 15
oh hey i should go back to line 15, remove that and go to line 15
oh hey i should go back to line 15, remove that and go to line 15
oh hey i should go back to line 15, remove that and go to line 15
oh hey i should go back to line 15, remove that and go to line 15
oh hey i should go back to line 15, remove that and go to line 15
oh hey i should go back to line 15, remove that and go to line 15
oh hey i should go back to line 15, remove that and go to line 15
oh hey i should go back to line 15, remove that and go to line 15
you can already see there is alot of work and alot of data being stored repeatedly
so how i solve it is by using a for loop emulating the recursion
so if i want to calculate the Factorial of a number instead of doing it like this :
int factorial(int n) {
if(n<=0)
return 1;
return n * factorial(n-1);
}
i do this :
int Factorial(int n) {
int r = 1;
for (; n > 0; n--) r *= n;
return r;
}
which is much better for your memory :)
hope that was helpful! this idea is something that i got from experiance
and trying diffrent stuff , it might not be perfect but i think its
much better
thanks for reading :)