-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collatz.cpp
45 lines (39 loc) · 890 Bytes
/
Collatz.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
#include <iostream>
#include <map>
using namespace std;
typedef map<int,int> collatz;
void printCollatz (collatz &, int );
int main(){
collatz c;
int start = 15;
printCollatz (c,start);
cout << "Done once" << endl;
printCollatz (c,7);
}
void printCollatz(collatz &c, int start){
collatz::iterator it;
int temp = start;
cout << temp << endl;
if ( temp % 2 == 0 ){
c[temp] = temp/2;
temp /= 2;
cout << temp << endl;
}
else{
c[temp] = temp*3 + 1;
temp = temp*3 + 1;
cout << temp << endl;
}
while ( c.find(temp) == c.end() ){
if ( temp % 2 == 0 ){
c[temp] = temp/2;
temp /= 2;
cout << temp << endl;
}
else{
c[temp] = temp*3 + 1;
temp = temp*3 + 1;
cout << temp << endl;
}
}
}