-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigInt.cpp
More file actions
269 lines (238 loc) · 5.75 KB
/
Copy pathBigInt.cpp
File metadata and controls
269 lines (238 loc) · 5.75 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Name Abrham Tamiru
// Class (CECS 325-02)
// Project Name (Prog 7 – 3n+1(BigInt))
// Due Date (5/4/2023)
//
// I certify that this program is my own original work. I did not copy any part of this program from
// any other source. I further certify that I typed each and every line of code in this program.
#include <iostream>
#include<limits.h>
#include <vector>
#include<algorithm>
#include <string>
using namespace std;
class BigInt {
private:
vector<int> v;
public:
BigInt(); // default constructor – set value to 0
BigInt(int); // int constractor
BigInt(string); //string constractor
BigInt operator+(BigInt); //adding two BigInt
BigInt operator++(); // prefix increament
BigInt operator++(int); // postfix increament
BigInt operator*(BigInt); // multiplication operator
BigInt half(); // return half the value
bool isOdd(); // check if it is odd
bool isEven(); // check if it is even
bool operator==(BigInt); // equal operator
bool operator<(BigInt); //less operator
friend ostream& operator<<(ostream&, const BigInt&) ;
};
BigInt::BigInt() {
v={};
}
BigInt::BigInt(int x) {
while (x > 0) {
v.push_back(x % 10);
x /= 10;
}
}
BigInt::BigInt(string s) {
for (int i = s.size() - 1; i >= 0; i--) {
v.push_back(s[i]);
}
}
BigInt BigInt::operator+(BigInt b) {
BigInt sum;
int carry = 0;
int i = 0, j = 0;
int digit;
while (i < v.size() || j < b.v.size() || carry > 0) {
int result = carry;
if (i < v.size()) {
result += v[i];
i++;
}
if (j < b.v.size()) {
result += b.v[j];
j++;
}
carry = result / 10;
digit = result % 10;
sum.v.push_back(digit);
}
return sum;
}
BigInt BigInt::operator++() {
BigInt result = operator+(1);
operator=(result);
return result;
}
BigInt BigInt::operator++(int) {
BigInt temp = *this;
*this = *this + 1;
return temp;
}
BigInt BigInt::operator*(BigInt b) {
BigInt product;
if (b == 0) {
return BigInt(0);
}
int carry = 0;
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < b.v.size() || carry > 0; j++) {
if (j < b.v.size()) {
carry += v[i] * b.v[j];
}
if (product.v.size() < i + j + 1) {
product.v.push_back(0);
}
carry += product.v[i + j];
product.v[i + j] = carry % 10;
carry /= 10;
}
}
while (product.v.size() > 1 && product.v.back() == 0) {
product.v.pop_back();
}
return product;
}
BigInt BigInt::half() {
BigInt result;
int carry = 0;
for (int i = v.size() - 1; i >= 0; i--) {
int val = carry * 10 + v[i];
int digit = val / 2;
carry = val % 2;
result.v.push_back(digit);
}
reverse(result.v.begin(), result.v.end());
while (result.v.size() > 1 && result.v.back() == 0) {
result.v.pop_back();
}
return result;
}
bool BigInt::isOdd()
{
return v[0]& 1;
}
bool BigInt::isEven()
{
return !isOdd();
}
bool BigInt::operator==( BigInt c)
{
if(v.size()!=c.v.size())
{
return false;
}
for (int i =0;i<v.size();i++){
if(v[i]!=c.v[i])
{
return false;
}
}
return true;
}
bool BigInt::operator<( BigInt d) {
if (v.size() < d.v.size())
{
return true;
}
else if (v.size() > d.v.size())
{
return false;
}
else
{
for (int i =0; i<v.size();i++)
{
if(v[i]<d.v[i])
{
return true;
}
}
return true;
}
}
std::ostream& operator<<(std::ostream& out, const BigInt& num)
{
if (num.v.size() > 8) {
int exponent = num.v.size() - 1;
out << num.v[num.v.size() - 1] << ".";
for (int i =num.v.size()-2 ; i >=num.v.size()-8 ; i--) // 7 digits after " ."
{
out << num.v[i];
}
out << "e" << exponent;
}
else {
for (int i = num.v.size() - 1; i >= 0; i--) {
out << num.v[i];
}
}
return out;
}
struct ThreeNp1 {
BigInt start;
BigInt steps;
BigInt max;
BigInt odd;
BigInt even;
};
void findthreeN(BigInt n, ThreeNp1 &Np1, bool printSteps=false)
{
if(printSteps)
{
cout << "->(" << n << ")";
}
if (n == 1) // BigInt::operator==( )
{
return;
}
else if (n.isEven()) // BigInt::isEven()
{
if (Np1.max < n) //BigInt::operator<( )
Np1.max = n;
Np1.even++; // BigInt::operator++( )
Np1.steps++;
n= n.half(); //BigInt::half( )
findthreeN(n, Np1);
}
else if(n.isOdd()) // BigInt::isodd()
{
if (Np1.max < n)
Np1.max = n;
Np1.odd++;
Np1.steps++;
n = n*BigInt(3)+BigInt(1); //BigInt::operator*( ) and BigInt::operator+( )
findthreeN(n, Np1);
}
else
{
cout<<"How I get here"<<endl;
}
}
void print(BigInt start, ThreeNp1 &Np1)
{
Np1.start = start; // initialize start field
findthreeN(start, Np1);
}
int main()
{
BigInt MAX(INT_MAX);
cout << "The largest integer is " << MAX << endl;
cout << "Twice the largest integer is " << MAX + MAX << endl;
BigInt start(INT_MAX);
bool printSteps=true;
ThreeNp1 N {start, 0, 0, 0, 0}; // initialize struct fields
print(start, N);
cout << endl;
cout << "start: " << N.start << endl;
cout << "steps: " << N.steps << endl;
cout << "max: " << N.max << endl;
cout << "odds: " << N.odd << endl;
cout << "evens: " << N.even << endl;
return 0;
}