-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion.cpp
More file actions
56 lines (39 loc) · 1.12 KB
/
Copy pathrecursion.cpp
File metadata and controls
56 lines (39 loc) · 1.12 KB
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
// Function calling itself is called recursion
// Application of Recursion
// 1. maintain stack
// there is master theory to calculate time complexity of recurrence realtion
#include <iostream>
using namespace std;
// int fact(int n)
// {
// int f = 1;
// for (int i = 0; i <= n; i++)
// {
// f = f * i;
// }
// }
// int main()
// {
// return 0;
// }
int fact(int n)
{
if (n == 1)
return 1;
return n * fact(n - 1);
}
int main()
{
int a = fact(2);
cout << "Value of a is :" << a;
return 0;
}
// time complexity is order of n. O(n)
// why iterative code is more effective than recursion code
// iterative code is more batter than recursion cause internally recursion create stack , takes more memory , takes more time cause cpu have to check
// same time complexity
// The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
// F(0) = 0, F(1) = 1
// F(n) = F(n - 1) + F(n - 2), for n > 1.
// Given n, calculate F(n).
// time complexity is O(2^n)