/** * Fibonacci, tail-recursively and not * @author gtowell * Created: March 2021 * **/ #include /** * A non tail recursive implementation of fibonacci * @param n1 -- a fibb number * @param n2 -- another fibb number * @return teh depth when stopped * **/ int fibTR(int n1, int n2) { int n3 = n1 + n2; if (n3>1000) return 1; return 1 + fibTR(n2, n3); } /** * A tail recursive implementation of fibonacci * @param n1 -- a fibb number * @param n2 -- another fibb number * @param i -- the current depth of the calculation * @return teh depth when stopped * **/ int fibTR(int n1, int n2, int i) { int n3 = n1 + n2; if (n3 > 1000) return i; return fibNTR(n2, n3, i + 1); } int main(int argc, char const *argv[]) { printf("%d\n", fibTR(1, 1)); printf("%d\n", fibNTR(1, 1, 1)); return 0; }