-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
85 lines (59 loc) · 2.14 KB
/
main.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
//change stdio color output
#define ANSI_COLOR_RESET "\x1b[0m"
#define ANSI_COLOR_RED "\x1b[31m"
//Get input from and handle characters as inputs
#define ASK_NUMBER(X) while(1){\
printf(" Please enter the value of "#X": ");\
if (scanf("%f", &X) != 1){\
printf(ANSI_COLOR_RED" Please enter a valid number\n"ANSI_COLOR_RESET);\
while (getchar() != '\n');} else break; }
//To measure time of execution
#include <time.h>
clock_t start, end;
double cpu_time_used;
//Normal fib
int fib(int n){
if(n < 2) return n;
return fib(n-1)+fib(n-2);
}
//Tail recursion
int fibTail(int n,int a,int b){
if(n == 0) return a;
if(n == 1) return b;
return fibTail(n-1,b,a+b);
}
int main()
{
//Without optimazition
start = clock();
printf("%d",fib(40));
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("\n fib without optimazition %lf",cpu_time_used);
//with tail recursion
start = clock();
printf("%d",fibTail(40,0,1));
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("\n fib with tail recursion optimazition %lf",cpu_time_used);
// //Dynamic programming
// int f[40];
// for (int i = 0; i < 40; i++) f[i] = -1;
// start = clock();
// printf("%d",fibDynamic(40));
// end = clock();
// cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
// printf("\n fib with dynamic programming %lf",cpu_time_used);
}
//Dynamic Programin
// int fib(int n){
// if(n < 2) return n;
// if(f[n] != -1) return f[n];
// return f[n] = fib(n-1)+fib(n-2);
// }