-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursive_fibonacci_number_calculator.cpp
More file actions
64 lines (60 loc) · 1.57 KB
/
recursive_fibonacci_number_calculator.cpp
File metadata and controls
64 lines (60 loc) · 1.57 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
#include <iostream>
using namespace std;
/// MAX. VALUE OF N IS 94, WHICH RESULTS IN 12200160415121876738.
unsigned long long int *fibonacci(int n, bool isRecursive)
{
if (n == 0)
{
unsigned long long int *result = new unsigned long long int[1];
result[0] = 0;
return result;
}
if (n == 1)
{
if (isRecursive)
{
unsigned long long int *result = new unsigned long long int[2];
result[0] = 0;
result[1] = 1;
return result;
}
else
{
unsigned long long int *result = new unsigned long long int[1];
result[0] = 1;
return result;
}
}
unsigned long long int *resultArray = fibonacci((n - 1), true);
unsigned long long int resultOfThis = (resultArray[0] + resultArray[1]);
if (isRecursive)
{
unsigned long long int *result = new unsigned long long int[2];
result[0] = resultArray[1];
result[1] = resultOfThis;
return result;
}
unsigned long long int *result = new unsigned long long int[1];
result[0] = resultOfThis;
return result;
}
int main()
{
int n = 0;
while (true)
{
while (n < 1)
{
cout << "Find the n. fibonacci number. n: ";
cin >> n;
if (n < 1)
{
cout << "Type a number greater than 0." << endl;
}
}
cout << n << ". fibonacci number is: " << fibonacci((n - 1), false)[0] << endl
<< endl;
n = 0;
}
return 0;
}