-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollatz2.cpp
More file actions
45 lines (35 loc) · 849 Bytes
/
collatz2.cpp
File metadata and controls
45 lines (35 loc) · 849 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
38
39
40
41
42
43
44
45
#include<iostream>
using namespace std;
int main() {
int input;
while (cin >> input) {
int original = input;
int count = 0;
int max = 1;
//Stop when it reaches 1
while (input != 1) {
//Apply collatz rule
if (input % 2 == 0) {
input /= 2;
}
else {
input = input * 3 + 1;
}
//Record maximum value
if (input > max) {
max = input;
}
//Increment number of steps
count++;
}
//Print number of steps and maximum value
cout << count << " steps for " << original << "\n";
cout << max << " is the maximal intermediate value for " << original << "\n";
}
return 0;
}
/*Commands
*$ echo {1..10000} | ./collatz | less
*$ echo {1..10000} | ./collatz | sort -rn | less
*$ echo {1..10000} | ./collatz | sort -rn | head -5
*/