-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathT02-Fibonacci_TailRec.c
53 lines (43 loc) · 1.36 KB
/
T02-Fibonacci_TailRec.c
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// The Fibonacci series is: 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
#include<stdio.h>
#include<stdlib.h>
/*
[BOTTOM-UP but Changing Context Window]
In each recurion, assume that the series starts 1 term ahead of the previous recursion - so compute (n-1)th term
Enter Accumulators! - they keep track of the first and second elements of each context
Eg: (for n=4),
Find the 4th in: [0, 1], 1, 2, 3, ... [0, 1], 1, 2, 3, ...
Find the 3rd in: [1, 1], 2, 3, ... 0, [1, 1], 2, 3, ...
Find the 2nd in: [1, 2], 3, ... 0, 1, [1, 2], 3, ...
Find the 1st in: [2, 3], ... 0, 1, 1, [2, 3,] ...
*/
int fibonacci_tailrec(int n, int first, int second){
//Invalid Input
if(n<1){
printf("\nI don't get that!\n");
exit(0);
}
// Base Case 1
if(n==1){
return first;
}
if(n==2){
return second;
}
else{
// Like computing a shortened version of the series
// Updates: new_first = second, new_second = first + second
return fibonacci_tailrec(n-1, second, first+second);
}
}
/*
Things to change:
- Do we need to go up to n==1?
- Wrapper (enclosing function) for the tailrec function
*/
void main(){
int n;
printf("Enter required fibonacci term: ");
scanf(" %d", &n);
printf("\nThe %dth fibonacci term is: %d\n", n, fibonacci_tailrec(n, 0, 1));
}