-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubmitCollatz.c++
127 lines (98 loc) · 2.14 KB
/
SubmitCollatz.c++
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
// ----------------------------
// projects/collatz/Collatz.c++
// Copyright (C) 2016
// Glenn P. Downing
// ----------------------------
// --------
// includes
// --------
#include <cassert> // assert
#include <iostream> // endl, istream, ostream
#include <sstream> // istringstream
#include <string> // getline, string
#include <vector>
#include <utility> // make_pair, pair
using namespace std;
// ------------
// collatz_read
// ------------
pair<int, int> collatz_read (const string& s) {
istringstream sin(s);
int i;
int j;
sin >> i >> j;
return make_pair(i, j);}
// ------------
// collatz_eval
// ------------
int collatz_eval (int i, int j) {
int ans = -1;
int count = 1;
int num;
if (j < i){
i ^= j;
j ^= i;
i ^= j;
}
for(int x = i; x <= j; ++x){
num = x;
while(num != 1){
if ((num & 1) == 0){
num >>= 1;
}
else {
num = (num << 1) + num + 1;
}
++count;
}
if (count > ans)
ans = count;
count = 1;
}
return ans;
}
// -------------
// collatz_print
// -------------
void collatz_print (ostream& w, int i, int j, int v) {
w << i << " " << j << " " << v << endl;}
// -------------
// collatz_solve
// -------------
void collatz_solve (istream& r, ostream& w) {
string s;
while (getline(r, s)) {
const pair<int, int> p = collatz_read(s);
const int i = p.first;
const int j = p.second;
const int v = collatz_eval(i, j);
collatz_print(w, i, j, v);}}
// ----
// main
// ----
int main () {
using namespace std;
collatz_solve(cin, cout);
return 0;}
/*
% g++ -pedantic -std=c++11 -Wall Collatz.c++ RunCollatz.c++ -o RunCollatz
% cat RunCollatz.in
1 10
100 200
201 210
900 1000
% RunCollatz < RunCollatz.in > RunCollatz.out
% cat RunCollatz.out
1 10 1
100 200 1
201 210 1
900 1000 1
% doxygen -g
// That creates the file Doxyfile.
// Make the following edits to Doxyfile.
// EXTRACT_ALL = YES
// EXTRACT_PRIVATE = YES
// EXTRACT_STATIC = YES
% doxygen Doxyfile
// That creates the directory html/.
*/