-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci.cpp
More file actions
88 lines (79 loc) · 2.48 KB
/
fibonacci.cpp
File metadata and controls
88 lines (79 loc) · 2.48 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
79
80
81
82
83
84
85
86
87
88
#include "fibonacci.h"
#include <iostream>
//
// Default constructor
//
Fibonacci::Fibonacci(void)
{
sequenceNumber = 0;
nextMostRecentPreceding.initialValueSetter(TWENTY_ONE_ZEROS_AND_A_ONE);
mostRecentPreceding.initialValueSetter(TWENTY_ONE_ZEROS_AND_A_ONE);
}
//
// Calculates the next number in a Fibonacci sequence.
//
VeryLong Fibonacci::iterativeNextInSequence(void)
{
VeryLong result;
switch(this->sequenceNumber++)
{
case 0:
result.initialValueSetter(TWENTY_TWO_ZEROS);
break;
case 1:
case 2:
result.initialValueSetter(TWENTY_ONE_ZEROS_AND_A_ONE);
break;
default:
result = this->mostRecentPreceding + this->nextMostRecentPreceding;
this->nextMostRecentPreceding = this->mostRecentPreceding;
this->mostRecentPreceding = result;
break;
}
return result;
}
void Fibonacci::recursivePrintSequence(VeryLong mostRecentPreceding,
VeryLong nextMostRecentPreceding,
unsigned sequenceMax)
{
VeryLong result;
int mrp;
int nmrp;
if(sequenceMax > 0)
{
mrp = mostRecentPreceding.convertLeastSignificantDigitToInt();
nmrp = nextMostRecentPreceding.convertLeastSignificantDigitToInt();
switch(mrp + nmrp)
{
case 0:
result.initialValueSetter(TWENTY_TWO_ZEROS);
mostRecentPreceding.initialValueSetter(TWENTY_ONE_ZEROS_AND_A_ONE);
break;
case 1:
result.initialValueSetter(TWENTY_ONE_ZEROS_AND_A_ONE);
if(1 == mrp)
{
mostRecentPreceding.initialValueSetter(TWENTY_TWO_ZEROS);
nextMostRecentPreceding.initialValueSetter(TWENTY_ONE_ZEROS_AND_A_ONE);
}
else
{
mostRecentPreceding.initialValueSetter(TWENTY_ONE_ZEROS_AND_A_ONE);
}
break;
default:
result = mostRecentPreceding + nextMostRecentPreceding;
nextMostRecentPreceding = mostRecentPreceding;
mostRecentPreceding = result;
break;
}
std::cout << result << std::endl;
this->recursivePrintSequence(mostRecentPreceding, nextMostRecentPreceding, sequenceMax - 1);
}
}
std::ostream& operator<<(std::ostream& os, const VeryLong val)
{
std::string num = val.num;
os << num.erase(0, std::min(num.find_first_not_of('0'), num.size()-1));
return os;
}