-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterations.cpp
122 lines (84 loc) · 2.7 KB
/
iterations.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
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
struct counters{int counter0, counter1;}; // a struct to hold two counters
typedef struct counters Struct;
Struct collatzIterations(int n); // obtains the number of iterations in Collatz conjecture
void convertToBase(int n, int base, vector<int>&); // converts a number to a base
void printVec(vector<int>); // prints the passed vector to console
ofstream file; // to write output to a file
int main(){
int n = 0; // the number that we go up to
int baseTo = 0;
vector<int> base; // will be the input number in the current base
Struct tmp; // a temporary struct to hold counters
file.open("output.txt");
file << "N,Iterations_Until_One,Iterations_Until_Less_Than_N,Base2,Base3,Base4" << endl;
cout << endl << "Up to which number should I analyze: ";
cin >> n;
cout << endl;
for(int i = 3; i <= n; i=i+2){ // only odd integers
if(i%100000==0){
cout << i << " ";
}
tmp = collatzIterations(i);
file << i << ",";
file << tmp.counter0 << ","; // column 2
file << tmp.counter1 << ","; // column 3
base.clear();
convertToBase(i, 2, base);
printVec(base);
base.clear();
file << ",";
convertToBase(i, 3, base);
printVec(base);
base.clear();
file << ",";
convertToBase(i, 4, base);
printVec(base);
base.clear();
file << endl;
}
cout << "Output completed in output.txt" << endl << endl;
return 0;
}
Struct collatzIterations(int n){
Struct counts;
int counter0 = 0; // number of iterations until n reaches 1
int counter1 = 0; // number of iterations until n is less than n
int originalN = n; // will store the original N
if(n==1){ // edge case
counts.counter0 = counter0;
counts.counter1 = counter1;
return counts;
}
while(n!=1){
if(n%2 == 0){ // if n is even
n = n/2;
}
else{ // if n is odd
n = 3*n+1;
}
counter0++; // increase counter of iterations to reach 1
if(n < originalN && counter1 == 0){
counter1 = counter0; // increase counter of iterations to be less than original n
}
}
counts.counter0 = counter0;
counts.counter1 = counter1;
return counts;
}
void convertToBase(int n, int base, vector<int>& baseVec){
int b;
while(n != 0){
b = n%base;
n = n/base;
baseVec.insert(baseVec.begin(), b); // adds b to beginning of vector
}
}
void printVec(vector<int> vec){
for(int i = 0; i < vec.size(); i++){
file << vec[i];
}
}