-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestingCode.cpp
More file actions
193 lines (165 loc) · 4.81 KB
/
TestingCode.cpp
File metadata and controls
193 lines (165 loc) · 4.81 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
#include <iostream>
#include <stdio.h>
#include <vector>
#include <memory>
#include <string>
#include <algorithm>
#include "BigInteger++.h"
unsignedBigInteger GCD(unsignedBigInteger x, unsignedBigInteger y) {
// GCD(x, y) = GCD(y, x % y)
if (x == 0)
return y;
if (y == 0)
return x;
while (y > 0) {
x %= y;
std::swap(x, y);
}
return x;
}
int main()
{
printf("Compiled correctly!\n\n");
printf("Checking variant operations:\n");
unsignedBigInteger number1(1234);
printf("number1 = ");
number1.PrintAsDecimal();
printf(" = ");
number1.PrintAsHex('\n');
unsignedBigInteger number2("5678");
printf("number2 = ");
number2.PrintAsDecimal();
printf(" = ");
number2.PrintAsHex('\n');
unsignedBigInteger number3("0x1A3b", 16);
printf("number3 = ");
number3.PrintAsDecimal();
printf(" = ");
number3.PrintAsHex('\n');
unsignedBigInteger number4(E18);
number4 *= 22;
printf("number4 = ");
number4.PrintAsDecimal();
printf(" = ");
number4.PrintAsHex('\n');
unsignedBigInteger number(HIGH_DWORD | LOW_DWORD), ONE(1);
number <<= 64;
number |= HIGH_DWORD | LOW_DWORD;
number -= 4;
++number;
number += 1;
number += ONE;
number = number + ONE;
number = number + 1;
printf("number5 = ");
number.PrintAsDecimal();
printf(" = ");
number.PrintAsHex('\n');
--number;
printf("number6 = ");
number.PrintAsDecimal();
printf(" = ");
number.PrintAsHex('\n');
printf("------------------------\n\n");
unsigned int exponent = 300;
unsignedBigInteger power2[5] = { 1,1,1,1,1 };
for (unsigned int i = 1; i <= exponent; i++) {
power2[0] *= 2;
power2[1] <<= 1;
unsignedBigInteger Temp = power2[2];
power2[2] += Temp;
}
power2[3] <<= exponent;
power2[4] = 2;
power2[4].FastPower(exponent);
printf("Calculation of 2^%d:\n", exponent);
printf("Using multiplication: ");
power2[0].PrintAsDecimal('\n');
printf("Using shifting : ");
power2[1].PrintAsDecimal('\n');
printf("Using addition : ");
power2[2].PrintAsDecimal('\n');
printf("Using shifting once : ");
power2[3].PrintAsDecimal('\n');
printf("Using power function: ");
power2[4].PrintAsDecimal('\n');
for (unsigned int i = 1; i <= exponent; i++) {
power2[0] /= 2;
power2[1] >>= 1;
}
power2[3] >>= exponent;
if (power2[0] != 1) {
printf("Division unsuccessful !\n");
printf("Found : ");
power2[0].PrintAsDecimal(' ');
printf("instead of 1\n");
}
if (power2[1] != 1) {
printf("Shifting unsuccessful !\n");
printf("Found : ");
power2[1].PrintAsDecimal(' ');
printf("instead of 1\n");
}
if (power2[3] != 1) {
printf("Shifting (once) unsuccessful !\n");
printf("Found : ");
power2[3].PrintAsDecimal(' ');
printf("instead of 1\n");
}
printf("------------------------\n\n");
printf("Printing factorials:\n");
const unsigned int N_fact = 100;
unsignedBigInteger fact[N_fact + 1];
fact[0] = 1;
for (int i = 1; i <= N_fact; i++) {
fact[i] = fact[i - 1] * i;
printf("%4d! = ", i);
fact[i].PrintAsDecimal('\n');
}
printf("------------------------\n\n");
printf("Printing the LCM of numbers from 1 to:\n");
const unsigned int N_LCM = 1000;
unsignedBigInteger LCM[N_LCM + 1];
unsignedBigInteger g;
LCM[1] = 1;
for (int i = 2; i <= N_LCM; i++) {
g = GCD(LCM[i - 1], unsignedBigInteger(i));
LCM[i] = (LCM[i - 1] / g) * i;
if (LCM[i] != LCM[i - 1]) {
printf("%4d => ", i);
LCM[i].PrintAsDecimal('\n');
}
}
printf("------------------------\n\n");
const unsigned int N_primes = 1000;
bool isPrime[N_primes + 1];
std::vector<unsigned int> primes;
memset(isPrime, true, sizeof isPrime);
isPrime[0] = false;
isPrime[1] = false;
for (unsigned long long i = 2; i <= N_primes; i++) {
if (!isPrime[i]) continue;
primes.push_back(i);
for (unsigned long long j = i * i; j <= N_primes; j += i)
isPrime[j] = false;
}
printf("Printing Primes upto %d:\n", N_primes);
unsignedBigInteger product = 1;
for (unsigned int p : primes) {
printf("%d\n", p);
product *= p;
}
printf("\nProduct of primes upto %d is:\n", N_primes);
product.PrintAsDecimal('\n');
printf("------------------------\n\n");
printf("Calculating 2^(2^x):\n");
unsignedBigInteger x(1), y;
for (unsigned int i = 1, j = 0; j <= 12; i <<= 1, j++) {
printf("x=%d, 2^%d =\n", j, i);
y = x;
y <<= i;
y.PrintAsDecimal('\n');
printf("\n");
}
return 0;
}