Created
July 13, 2016 12:56
-
-
Save missingdays/31dd13d669fa74aee7a847f88ac6c542 to your computer and use it in GitHub Desktop.
Recursion example
This file contains hidden or 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
int factorial_recursion(int n){ | |
if(n < 2){ | |
return 2; | |
} | |
return n * factorial_recursion(n-1) | |
} | |
int factorial(int n){ | |
int m = 1; | |
for(int i = 2; i <= n; i++){ | |
m *= i; | |
} | |
return m; | |
} | |
int fibonacci_recursion(int n){ | |
if(n == 0){ | |
return 0; | |
} | |
if(n == 1){ | |
return 1; | |
} | |
return fibonacci_recursion(n-1) + fibonacci_recursion(n-2); | |
} | |
int fibonacci(n){ | |
int a = 0, b = 1; | |
if(n == 0){ | |
return a; | |
} | |
for(int i = 0; i < n-1; i++){ | |
int tmp = b; | |
b = a + b; | |
a = tmp; | |
} | |
return b; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment