-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibonacci.cpp
52 lines (43 loc) · 1.06 KB
/
Fibonacci.cpp
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
// 1. Write a Fibonacci function using recursion
// 2. Write a Fibonacci function using iteration
// 3. Write a unit test to check above implemented Fibonacci functions
#include <iostream>
using namespace std;
int FibRec(int i) {
if (i < 2) {
return i;
}
return (FibRec(i - 2) + FibRec(i - 1));
}
int FibIt(int i) {
if (i == 0) {
return 0;
}
int old = 0;
int cur = 1;
for (int k = 2; k < i; k++) {
int tmp = cur;
cur = old + cur;
old = tmp;
}
return (old + cur);
}
bool FibTest() {
int et[] = { 0,1,1,2,3,5,8,13,21 };
//unit test
bool passed = true;
for (int i = 0; i < sizeof(et) / sizeof(int); i++) {
if (FibRec(i) != et[i] || FibIt(i) != et[i]) {
cout << "FAILED. i=" << i << endl;
cout << " Recursive: expected=" << et[i] << ", actual=" << FibRec(i) << endl << "----------------------------" << endl;
cout << " Itterative: expected=" << et[i] << ", actual=" << FibIt(i) << endl << "----------------------------" << endl;
passed = false;
break;
}
}
if (passed) {
cout << "PASSED." << endl;
return true;
}
return false;
}