fibonacci/fibo.c

35 lines
627 B
C
Raw Normal View History

2022-04-29 08:45:42 +03:00
// Fibonacci number generator in ANSI C using recursion
#include <stdio.h>
#include <time.h>
2022-04-29 09:31:15 +03:00
#define COUNT 30
2022-04-29 08:45:42 +03:00
long Fibo (long x);
int main (int argc, const char * argv[]) {
2022-04-29 09:31:15 +03:00
2022-04-29 08:45:42 +03:00
long fibo;
2022-04-29 09:31:15 +03:00
int i;
2022-04-29 08:45:42 +03:00
clock_t start, done;
start = clock();
2022-04-29 09:31:15 +03:00
printf("Lasketaan fibo %d \n", COUNT);
2022-04-29 08:45:42 +03:00
2022-04-29 09:31:15 +03:00
for (i = 1; i <= COUNT; i++) {
2022-04-29 08:45:42 +03:00
fibo = Fibo (i);
2022-04-29 09:31:15 +03:00
printf ("Fibo: %d - %ld\n",i, fibo);
2022-04-29 08:45:42 +03:00
}
done = clock();
2022-04-29 09:31:15 +03:00
printf("Laskenta kesti: %f sec\n",(done-start)/(double)(CLOCKS_PER_SEC));
2022-04-29 08:45:42 +03:00
return 0;
}
long Fibo (long x) {
if (x < 3)
return 1;
else
return Fibo (x-1) + Fibo (x-2);
}