-
-
Save JoaoGFarias/8968958 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cpdef int fib(int n): | |
cpdef fibAux(int n, int a, int b): | |
if n == 0: | |
return a | |
else: | |
return fibAux(n-1,a+b,a) | |
if n == 2 or n == 1: | |
return n | |
elif n > 0: | |
return fibAux(n,0,1) | |
else: | |
raise ValueError('Fibonacci need be a natural number.') | |
if __name__ == '__main__': | |
print(fib(992)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here, you can see Fibonacci with tail-call recursion.
It allows the compiler to do tail-call optimization and don't blow up
the call stack when dealing with deep recursions.