-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLucasNumbers3
More file actions
92 lines (74 loc) · 2.1 KB
/
LucasNumbers3
File metadata and controls
92 lines (74 loc) · 2.1 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
89
90
91
92
//============================================================================
// Name : LucasNumbers.cpp
// Author : Imogen Cleaver-Stigum & Jyalu Wu
// Version :
// Copyright : Your copyright notice
// Description : Lucas Number Calculator C++
//============================================================================
#include <iostream>
#include <time.h>
#include "LucasNumbers.h"
using namespace std;
int LucasNumbersSlow(int n) {
if (n == 0) {
cout << "L(0) = 2" << endl;
return 2;
}
else if (n == 1) {
cout << "L(1) = 1" << endl;
return 1;
}
else {
int main(int argc, char** argv) {
clock_t t0 = clock();
int n = 0;
if (argc > 1) {
n = atoi(argv[1]);
}
// part 2 - Lucas nums up to n
clock_t totalTime = LucasNumbers(n);
t0 = clock() - t0;
return 0;
}
clock_t LucasNumbersConstantTime(int n){
clock_t startTime = clock();
clock_t thisTime = startTime;
float prevTime = ((float) startTime)/CLOCKS_PER_SEC;
if (n >= 0) {
thisTime = clock() - startTime;
LucasPrint(0, 2, thisTime, prevTime);
prevTime = ((float) thisTime)/CLOCKS_PER_SEC;
}
if (n >= 1) {
thisTime = clock() - startTime;
LucasPrint(1, 1, thisTime, prevTime);
}
if (n > 1) {
thisTime = nextLucasNumber(n, 2, 1, 2, startTime, thisTime, prevTime);
}
return thisTime;
}
clock_t nextLucasNumber(int targetN, int currentN, long long nMinusOne, long long nMinusTwo,
clock_t startTime, clock_t thisTime, float prevTime) {
long long thisNum = nMinusOne + nMinusTwo;
prevTime = ((float) thisTime)/CLOCKS_PER_SEC;
thisTime = clock() - startTime;
LucasPrint(currentN, thisNum, thisTime, prevTime);
if (targetN > currentN) {
thisTime = nextLucasNumber(targetN, currentN+1, thisNum, nMinusOne,
startTime, thisTime, prevTime);
}
return thisTime;
}
void LucasPrint (int n, long long val, clock_t time, float prevTime) {
float tTime = ((float) time) / CLOCKS_PER_SEC;
cout << "L(" << n << ") = " << val << "\t\t in " << tTime;
cout << "\tseconds.";
if (n >= 1) {
cout << "\tprevious time: " << prevTime;
cout << "\t\tRatio: " << (tTime / prevTime) << endl;
}
else {
cout << endl;
}
}