Recursive Sequence of Death [Python] - Printable Version +- Makestation (https://makestation.net) +-- Forum: Technical Arts (https://makestation.net/forumdisplay.php?fid=45) +--- Forum: Software (https://makestation.net/forumdisplay.php?fid=104) +--- Thread: Recursive Sequence of Death [Python] (/showthread.php?tid=1916) |
Recursive Sequence of Death [Python] - Darth-Apple - April 20th, 2018 We've been working on recursive sequences in python in classes lately, and I was a little surprised at how much math actually makes a difference in your ability to optimize code. I wrote a little demonstration for one of my assignments. The fibonacci sequence makes no sense to me, but essentially, it goes like this. 0, 1, 1, 2, 3, 5, 8, 13, 21, ... Each digit is the sum of the two previous digits. Pretty simple. We could definitely extrapolate this as far as we wanted to and get an infinitely large number of digits, but I won't go there. Apparently this is some sort of important sequence in computer science and I don't know why, but you should be able to see the pattern easily enough. Say we wanted to know what, say, the 20th digit in the sequence was? It turns out that the method for finding this is a little bit more complicated than you'd think. It's rather tedious to go and actually add up all of the numbers. And particularly if you're going to use a recursive sequence, as fast as we think computers are these days, trying to go past about the 40th term or so in the sequence will crash my macbook because literally going through and generating this sequence is that tedious for a computer. Maybe yours might get a tad bit higher but I'd be willing to bet you won't get past 50 in Python. Okay, but why in the world is this so freaking slow? It's because it's recursive. If it wants to find the 5th number in the sequence, it first must find the 4th and the 3rd, and add those up. But because it doesn't know what the 4th and the 3rd are, in order to find the 4th, it must find the 3rd and the 2nd. And in order to find the 3rd, it must find the 2nd and the first. And it has to go through all of these steps before it can give a simple answer. Although this is easy for small numbers, as you can probably imagine, this starts to grow exponentially once you start finding, say, the 20th, or the 30th, or the 35th number in a sequence. The next thing you know, your computer can't even find the 40th term in a simple sequence without crashing, where you could probably do it by hand in a matter of a few minutes. There are two ways to speed this up. Instead of calling the same function over and over again and creating a gigantic tree of function calls to recalculate the same data over and over and over again, you can either save what each digit is as you go, and only calculate if you haven't saved that particular digit yet, or you can use something called an exact solution (a simple formula you make out of the recursive sequence using some sort of voodoo magic) to do the trick. By saving these numbers, it prevents the results from growing exponentially because it never has to recalculate values it's gone over before. You can easily calculate, say, the 4000th term with this method. I demonstrated all three methods with the following code. It calculates the 35th number in the sequence. Enough to take a few seconds on the recursive calculation, but not enough to crash my Macbook. Hopefully it won't crash yours. Code: import sys This will run in Python 3 if you want to save it into a .py file and run it. And I attached a file with this post too. This page (yes that's a link) pretty much describes what we spent an hour or two doing in class to optimize this. It's my very first semester in this new program, I haven't even taken Calculus yet, and I already feel like my head is hurting. Needless to say, you can teach yourself to program, but it's hard to teach yourself everything. I feel that, even solving simple problems, I have learned a lot. RE: Recursive Sequence of Death [Python] - SpookyZalost - April 20th, 2018 haha nice! python is currently my favorite language just because of it's flexibility. have fun learning to tame the snake RE: Recursive Sequence of Death [Python] - Darth-Apple - April 20th, 2018 Haha thank you! Yeah Python is pretty easy all things considered. It's so nice to code that I almost wish it was more popular in the web sphere of things. As much as I don't particularly care for PHP, it's what we use here because it's what is widely supported. Java is probably going to be what I have to tackle next, unfortunately. And Java is no fun, from what little I have done so far. RE: Recursive Sequence of Death [Python] - SpookyZalost - May 15th, 2018 @Darth: ever try combining python with I3 and some Texmode User interface stuff? if you've ever played hacknet it looks really close to the game's interface, crazy stuff RE: Recursive Sequence of Death [Python] - Darth-Apple - May 15th, 2018 Hmm, that's the first I've really heard of it. I've done a fair amount of stuff in Turtle (trivially easy), might have to check it out. RE: Recursive Sequence of Death [Python] - SpookyZalost - May 16th, 2018 Using python programs while running this window manager. Compare to a screenshot of this game. crazy right? but super hard core functional. RE: Recursive Sequence of Death [Python] - Lain - February 27th, 2019 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) Now, iteratively: Code: uint_32 a, 0; But the call-stack will look more like: Code: call fib(20) 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. RE: Recursive Sequence of Death [Python] - bestforums - April 19th, 2019 Sounds interesting. Thanks for all of this information. |