Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Recursive Sequence of Death [Python]

#7
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:
Code:
call fib(20)
   call fib(19)
       call fib(18)
           call fib(17)
               ...
               return 0, 1
           }
       }
   }
}
Returning whatever value.

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)
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:
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.
Reply


Messages In This Thread
Recursive Sequence of Death [Python] - by Darth-Apple - April 20th, 2018 at 5:46 PM
RE: Recursive Sequence of Death [Python] - by SpookyZalost - April 20th, 2018 at 8:19 PM
RE: Recursive Sequence of Death [Python] - by Darth-Apple - April 20th, 2018 at 9:26 PM
RE: Recursive Sequence of Death [Python] - by SpookyZalost - May 15th, 2018 at 12:14 PM
RE: Recursive Sequence of Death [Python] - by Darth-Apple - May 15th, 2018 at 5:24 PM
RE: Recursive Sequence of Death [Python] - by Lain - February 27th, 2019 at 6:07 PM
RE: Recursive Sequence of Death [Python] - by bestforums - April 19th, 2019 at 3:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python programmers? Darth-Apple 20 14,289 November 16th, 2020 at 6:02 AM
Last Post: tc4me
  Cython - Python at the Speed of C Darth-Apple 0 1,697 April 11th, 2020 at 1:07 PM
Last Post: Darth-Apple



Users browsing this thread: 1 Guest(s)

Dark/Light Theme Selector

Contact Us | Makestation | Return to Top | Lite (Archive) Mode | RSS Syndication 
Proudly powered by MyBB 1.8, © 2002-2024
Forum design by Makestation Team © 2013-2024