Campbell Ritchie wrote:Same as you would for any recursion: use 1 and 2 for your base case. In Java® I would writeYou will of course get an “infinite” recursion if i ≤ 0
The quotes around infinite are correct, because it would definitely end because of overflow. The method will be recursively called until the argument becomes Integer.MIN_VALUE or Integer.MIN_VALUE + 1. When you subtract 2 from that it will overflow to Integer.MAX_VALUE - 1 or Integer.MAX_VALUE respectively.
Of course you'll run into a StackOverflowError long before it even gets there.
In addition to the other ways of hitting some limit, Python also has a builtin recursion default limit of 1000. It can be changed by using sys.setrecursionlimit() if desired. However, for most Python programs, hitting that limit (or one of the others) means that the algorithm can be improved to avoid hitting any of these limits. 🙂