Exception in thread “main” java.lang.StackOverflowError

Your algorithm is fine. However int is too small for your computations, it fails for this input: At some point integer overflows to negative value and your implementation goes crazy, recursing infinitely. Change int num to long num and you’ll be fine – for some time. Later you’ll need BigInteger. Note that according to Wikipedia on Collatz conjecture (bold mine): The longest progression for … Read more

how to implement quick sort algorithm in C++

Your consideration is wrong. The value of r does not change, since it is given as value to the Quicksort function(not a reference). You handle the ranges with p,q such that p is the first index in the range and q the first index not in the range. Thus, your calls were wrong: Here is the complete example. I used std::swap to change elements and ans std::vector … Read more

What is the maximum recursion depth in Python, and how to increase it?

It is a guard against a stack overflow, yes. Python (or rather, the CPython implementation) doesn’t optimize tail recursion, and unbridled recursion causes stack overflows. You can check the recursion limit with sys.getrecursionlimit: and change the recursion limit with sys.setrecursionlimit: but doing so is dangerous — the standard limit is a little conservative, but Python … Read more

What is the maximum recursion depth in Python, and how to increase it?

It is a guard against a stack overflow, yes. Python (or rather, the CPython implementation) doesn’t optimize tail recursion, and unbridled recursion causes stack overflows. You can check the recursion limit with sys.getrecursionlimit: and change the recursion limit with sys.setrecursionlimit: but doing so is dangerous — the standard limit is a little conservative, but Python … Read more