-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path014_CollatzSequence.cpp
More file actions
37 lines (29 loc) · 888 Bytes
/
014_CollatzSequence.cpp
File metadata and controls
37 lines (29 loc) · 888 Bytes
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
// https://projecteuler.net/problem=14
#include <iostream>
#include <vector>
using namespace std;
int main()
{
long int n = 1000000;
long int maxLength = 0;
long int maxstart = 0;
for (long int i = n; i > 1; i--) {
vector<long int> collatz;
long int current = i;
while (current > 1) {
collatz.push_back(current);
if (current % 2 == 0)
current = current / 2;
else
current = 3 * current + 1;
}
collatz.push_back(1);
if (collatz.size() > maxLength){
maxLength = collatz.size();
maxstart = i;
}
}
cout << "Maximum Collatz sequence length from 1 to " << n << " is: " << maxLength << endl;
cout << "Maximum Collatz sequence length from 1 to " << n << " starts from: " << maxstart << endl;
return 0;
}