Skip to content

Instantly share code, notes, and snippets.

@simonmysun
Last active December 14, 2015 01:19
Show Gist options
  • Save simonmysun/5005078 to your computer and use it in GitHub Desktop.
Save simonmysun/5005078 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<time.h>
int fibnor(int a, int b, int n);
int fibfun(int a, int b, int n);
int fibweak(int a, int b, int n);
int main() {
int a, b, n, res, k;
int t = 10000;
while(scanf("%d%d%d", &a, &b, &n) != - 1) {
clock_t ts, te;
ts = clock(NULL);
for(int i = 1; i <= t; i ++ ) {
k ++ ;
res = fibnor(a, b, n);
}
te = clock(NULL);
printf("nor:%d %ld\n", res, te - ts);
ts = clock(NULL);
for (int i = 1; i <= t; i ++ ) {
k ++ ;
res = fibfun(a, b, n);
}
te = clock(NULL);
printf("fun:%d %ld\n", res, te - ts);
ts = clock(NULL);
for (int i = 1; i <= t;i ++ ) {
k ++ ;
res = fibweak(a, b, n);
}
te = clock(NULL);
printf("weak:%d %ld\n", res, te - ts);
printf("%d\n", k);
}
return 0;
}
int fibnor(int a, int b, int n) {
int tmp;
for(int i = 0; i < n; i ++ ) {
tmp = b;
b = a + b;
a = tmp;
}
return a;
}
int fibfun(int a, int b, int n) {
if(n <= 1) {
return b;
}
else {
return fibfun(b, a + b, n - 1);
}
}
int fibweak(int a, int b, int n) {
if (n == 1) {
return a;
} else if (n == 2) {
return b;
} else {
return fibweak(a, b, n - 1) + fibweak(a, b, n - 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment