Browse Source

Add Python version and clean code

master
Nyymix 2 months ago
parent
commit
624d4527a8
  1. 2
      README.md
  2. 15
      fibo.c
  3. 22
      fibo.py

2
README.md

@ -1,3 +1,3 @@
# fibonacci
Fibonacci number generator in ANSI C using recursion
Fibonacci number generator in ANSI C, Python using recursion

15
fibo.c

@ -2,26 +2,27 @@
#include <stdio.h>
#include <time.h>
#define COUNT 30
long Fibo (long x);
int main (int argc, const char * argv[]) {
long fibo;
int i, count;
int i;
clock_t start, done;
count = 40;
start = clock();
printf("Lasketaan fibo %d \n", count);
printf("Lasketaan fibo %d \n", COUNT);
for (i = 1; i <= count; i++) {
for (i = 1; i <= COUNT; i++) {
fibo = Fibo (i);
printf ("%d - %ld\n",i, fibo);
printf ("Fibo: %d - %ld\n",i, fibo);
}
done = clock();
printf("aika: %f s\n",(done-start)/(double)(CLOCKS_PER_SEC));
printf("Laskenta kesti: %f sec\n",(done-start)/(double)(CLOCKS_PER_SEC));
return 0;
}

22
fibo.py

@ -0,0 +1,22 @@
# Fibonacci number generator in Python using recursion
import time
COUNT = 30
def fibo(i):
if i <= 1:
return i
else:
return(fibo(i-1) + fibo(i-2))
def main():
print("Lasketaan fibo: %d" % COUNT)
start_time = time.time()
for x in range(COUNT):
print("Fibo: %2d = %d" %(x, fibo(x)))
print("Laskenta kesti %.5f sec" % (time.time() - start_time))
if __name__ == '__main__':
main()
Loading…
Cancel
Save