Created
October 5, 2020 06:18
-
-
Save phabee/3680efcdd7e7ff833b03f34967c360f0 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
#include <stdio.h> | |
#include <time.h> | |
#include <sys/time.h> | |
int fibonacci(int n); | |
int main() { | |
clock_t begin, end; | |
float z; | |
int a; | |
begin = clock(); | |
a = fibonacci(30); | |
end = clock(); | |
z = (end - begin)/CLOCKS_PER_SEC; | |
printf("Fibonacci number(30): (%d) in %d s = %d / %d s", a, z, (end - begin), CLOCKS_PER_SEC); | |
} | |
int fibonacci(int n) { | |
if (n > 2) | |
return fibonacci(n-2) + fibonacci(n-1); | |
if (n == 2 || n == 1) | |
return 1; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple demonstration to calculate the n-th element of a fibonacci sequence in C using recursion.