-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem14intel.cpp
173 lines (132 loc) · 3.89 KB
/
Problem14intel.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <iostream>
#include <chrono>
#include <map>
void Solve() {
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
const int number = 100000000;
const int start = number / 2 - ((number/2) % 96);
int sequenceLength = 0;
int startingNumber = 0;
unsigned long long sequence;
int modulos[16] = {1, 7, 9, 15, 25, 27, 31, 33, 39, 43, 57, 63, 73, 75, 79, 91};
for (int j = 0; j < 16; j++) {
for (int n = start; n <= number; n += 96) {
int i = n + modulos[j];
int length = 1;
sequence = i;
while (sequence > 1) {
//std::cout << length << " " << sequence << std::endl;
if (sequence % 2) {
sequence += (sequence << 1) + 1;
} else {
sequence >>= 1;
}
length++;
}
//Check if sequence is the best solution
if (length > sequenceLength) {
sequenceLength = length;
startingNumber = i;
}
}
}
std::chrono::steady_clock::time_point end= std::chrono::steady_clock::now();
std::cout << startingNumber << " " << sequenceLength << " ";
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << std::endl;
}
/*
void SolveWithCache() {
const int number = 10000000;
int sequenceLength = 0;
int startingNumber = 0;
long sequence;
int cache[number + 1];
//Initialise cache
for (int i = 0; i <= number; i++) {
cache[i] = -1;
}
cache[1] = 1;
for (int i = number / 2 + 1; i <= number; i += 2) {
sequence = i;
int k = 0;
while (sequence != 1 && sequence >= i) {
k++;
sequence = sequence % 2 == 0 ? sequence >> 1 : sequence * 3 + 1;
}
//Store result in cache
cache[i] = k + cache[sequence];
//Check if sequence is the best solution
if ((cache[i] > -1) && (cache[i] > sequenceLength)) {
sequenceLength = cache[i];
startingNumber = i;
}
}
std::cout << "The starting number " << startingNumber << " produces a sequence of " << sequenceLength << std::endl;
std::cout << "Solution took " << std::endl;
}
public std::map<long, int> recCache;
void SolveWithRecursion() {
const int number = 1000000;
int longestSeqIndex = 0;
int longestSeq = 0;
int sequenceLength = 0;
recCache[1] = 1;
for (int i = 2; i <= number; i++) {
sequenceLength = getLength(i);
//Check if sequence is the best solution
if (sequenceLength > longestSeq) {
longestSeqIndex = i;
longestSeq = sequenceLength;
}
}
std::cout << "The starting number " << longestSeqIndex << " produces a sequence of " << longestSeq << std::endl;
std::cout << "Solution took " << std::endl;
}
private int getLength(long i) {
int chainLength = 0;
if (recCache.count(i)) return recCache[i];
if (i % 2 == 0) {
chainLength = 1 + getLength(i/2);
} else {
chainLength = 1 + getLength(3*i +1);
}
recCache[i] = chainLength;
return chainLength;
}
private int recArrayCache;
void SolveWithRecursionArray() {
const int number = 10000000;
int longestSeqIndex = 0;
int longestSeq = 0;
int sequenceLength = 0;
recArrayCache = int[number+1];
recArrayCache[1] = 1;
for (int i = 2; i <= number; i++) {
sequenceLength = getArrayLength(i);
//Check if sequence is the best solution
if (sequenceLength > longestSeq) {
longestSeqIndex = i;
longestSeq = sequenceLength;
}
}
std::cout << "The starting number " << longestSeqIndex << " produces a sequence of " << longestSeq << std::endl;
std::cout << "Solution took " << std::endl;
}
private int getArrayLength(long i) {
int chainLength = 0;
if (i < recArrayCache.Length && recArrayCache[i] != 0) return recArrayCache[i];
if (i % 2 == 0) {
chainLength = 1 + getArrayLength(i / 2);
} else {
chainLength = 1 + getArrayLength(3 * i + 1);
}
if (i < recArrayCache.Length) recArrayCache[i] = chainLength;
return chainLength;
}*/
int main () {
Solve();
//new Problem14().SolveWithCache();
//new Problem14().SolveWithRecursion();
//new Problem14().SolveWithRecursionArray();
return 0;
}