-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci .cpp
More file actions
78 lines (64 loc) · 1.36 KB
/
Fibonacci .cpp
File metadata and controls
78 lines (64 loc) · 1.36 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <vector>
#include <wtypes.h>
#include <iostream>
#include <cstdlib>
#include <deque>
#include <stack>
#include <stddef.h>
#include <vector>
#include <set>
#include <cassert>
using namespace std;
int fib(int x) // recursive Fibonacci_up_to_n_step
{
if ( x < 2 )
return x;
else
return( fib(x - 1) + fib(x - 2) );
}
int Fibonacci(int N) // dynamic programming Fibonacci_up_to_n_step
{
int a = 0, b = 1, next, i;
if ( N == 0 )
return a;
for ( i = 2; i <= N; i++ )
{
next = a + b;
a = b;
b = next;
}
return b;
}
int Fibonacci_Array(int N) // dynamic programming Fibonacci_up_to_n_step
{
int* FibArray = (int*)malloc(( N + 2 ) * sizeof(int));
memset(FibArray, 0, ( N + 2 ) * sizeof(int));
int i;
FibArray[ 0 ] = 0;
FibArray[ 1 ] = 1;
for ( i = 2; i <= N; i++ )
{
FibArray[ i ] = FibArray[ i - 1 ] + FibArray[ i - 2 ];
}
return FibArray[ N ];
}
void Fibonacci_Up_to_a_Certain_Number(int N) // dynamic programming
{
int a = 0;
int b = 1;
while ( a <= N )
{
std::cout << a << " ";
int next = a + b;
a = b;
b = next;
}
}
int main()
{
printf("%d %d %d" , fib(20) , Fibonacci(20) , Fibonacci_Array(20));
//int arr[] = {0 , 0 , 1 , 1 , 1};
//printf("%d", goldenLeader(arr, sizeof(arr) / sizeof(arr[ 0 ])));
return 0;
}