Ask coding questions

← Back to all posts
Why this program is taking too much time?
SourabhPrasad (0)

It's not even showing compile error!!!!!

Answered by 19wintersp (1120) [earned 5 cycles]
View Answer
19wintersp (1120)

Aren't you making about 1.5e29 calls to fib? See this StackOverflow question for tips on making it faster. It's in Python, but the same principles apply.

FlaminHotValdez (442)

It is just really inefficient because you are recursing so many times for such a big number, the recursion ends up stacking like 2^98 times(I'm not sure about the exact value it's probably a lot less but that's just a guess). If you want to have a method to get Fibonacci for big numbers, I recommend you don't use recursion, because it calls the function so many times, especially for larger numbers.

EpicGamer007 (1643)

it is because it is taking a long time to execute

SourabhPrasad (0)

@EpicGamer007 for smaller it's taking less time, but how to resolve time complexity for bigger number.

FlaminHotValdez (442)

@SourabhPrasad You shouldn't use recursion, it's calling the same function over and over again, somewhere around 2^98.

SourabhPrasad (0)

@FlaminHotValdez but I have to do only using reccursion!

FlaminHotValdez (442)

@SourabhPrasad Oh...then I think you're about as optimized as it gets.