summaryrefslogtreecommitdiff
path: root/fib.c
blob: 6f510c237a4388fd0b268a018b198b1ca008869c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>

static long fib_internal(int n, long a, long b) {
    if (n == 0) {
        return a;
    } else if (n == 1) {
        return b;
    } else {
        __attribute__((musttail))
        return fib_internal(n-1, b, a + b);
    }
}

long fib(int n) {
    return fib_internal(n, 0, 1);
}

void test(int n) {
    printf("fib [%d] = [%ld]\n", n, fib(n));
}

// static long recursion_depth(long n, long max) {
//     if (n >= max) {
//         return n;
//     }
//     __attribute__((musttail))
//     return recursion_depth(n+1, max);
// }

int main(void) {
    for (int i=1; i<300; i++) {
        test(i);
    }
    //printf("%ld\n", recursion_depth(0, 1000000000));
}