Recursive print Factorial

return factValue * printFactorial(factValue – factCounter); I assume that you should be using the “next” values instead of these. Edit: Also note that the function takes two parameters and is void. Returning factValue times void doesn’t make sense.

Java recursive Fibonacci sequence

In fibonacci sequence each item is the sum of the previous two. So, you wrote a recursive algorithm. So, Now you already know fibonacci(1)==1 and fibonacci(0) == 0. So, you can subsequently calculate the other values. Now, And from fibonacci sequence 0,1,1,2,3,5,8,13,21…. we can see that for 5th element the fibonacci sequence returns 5. See here for Recursion Tutorial.

Determining complexity for recursive functions (Big O notation)

The time complexity, in Big O notation, for each function: This function is being called recursively n times before reaching the base case so its O(n), often called linear. This function is called n-5 for each time, so we deduct five from n before calling the function, but n-5 is also O(n). (Actually called order of n/5 times. … 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 stackframes can … Read more

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