Add Python version and clean code
This commit is contained in:
parent
4fac86fe54
commit
624d4527a8
3 changed files with 31 additions and 8 deletions
|
@ -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
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
Normal file
22
fibo.py
Normal file
|
@ -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…
Reference in a new issue