Skip to content

Instantly share code, notes, and snippets.

@phabee
Created October 5, 2020 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phabee/3680efcdd7e7ff833b03f34967c360f0 to your computer and use it in GitHub Desktop.
Save phabee/3680efcdd7e7ff833b03f34967c360f0 to your computer and use it in GitHub Desktop.
#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;
}
@phabee
Copy link
Author

phabee commented Oct 5, 2020

Simple demonstration to calculate the n-th element of a fibonacci sequence in C using recursion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment