Last active
February 22, 2021 23:34
-
-
Save danielomiya/5996b8b3debb525fb6631d080f37dc6a to your computer and use it in GitHub Desktop.
Recursive Fibonacci algorithm in C
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
| #include <stdio.h> | |
| #include <stdlib.h> | |
| int fib(int n); | |
| int main(int argc, char **argv) { | |
| // compiling: gcc fib.c -o fib | |
| // exec with: ./fib <n> | |
| int i_num; | |
| if (argc <= 1) { | |
| printf("Usage: ./fib <n>\n"); | |
| return 1; | |
| } | |
| i_num = atoi(argv[1]); | |
| printf("O %io. termo de fib eh %i\n", i_num, fib(i_num - 1)); | |
| return 0; | |
| } | |
| int fib(int n) { | |
| // 0-indexed | |
| if (n <= 1) return 1; | |
| return fib(n - 2) + fib(n - 1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment