aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/fib.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/fib.c')
-rw-r--r--test/fib.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/test/fib.c b/test/fib.c
index 6254a5f..eadce03 100644
--- a/test/fib.c
+++ b/test/fib.c
@@ -1,6 +1,6 @@
unsigned fib(unsigned x) {
unsigned r = 0, q = 1;
- while (x--) {
+ while (x-- > 1) {
unsigned s = r + q;
r = q;
q = s;
@@ -8,12 +8,18 @@ unsigned fib(unsigned x) {
return q;
}
+unsigned fibr(unsigned x) {
+ if (x < 2) return x;
+ return fibr(x-1) + fibr(x-2);
+}
+
int atoi(const char *);
int printf(const char *, ...);
int main(int argc, char **argv) {
unsigned n = argv[1] ? atoi(argv[1]) : 10;
- printf("fib(%u) = %u\n", n, fib(n));
+ printf("fib(%u) = %u\n", n, fib(n));
+ printf("fibr(%u) = %u\n", n, fibr(n));
}
/* vim:set ts=3 sw=3 expandtab: */