-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuc2.cpp
306 lines (289 loc) · 8.74 KB
/
uc2.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#ifdef _WIN32
#include "boinc_win.h"
#else
#include "config.h"
#include <cstdio>
#include <cctype>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <csignal>
#include <unistd.h>
#endif
#include "str_util.h"
#include "util.h"
#include "filesys.h"
#include "boinc_api.h"
#include "mfile.h"
#include "graphics2.h"
#include "uc2.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <cmath>
using namespace std;
using std::string;
#define CHECKPOINT_FILE "upper_case_state"
#define INPUT_FILENAME "in"
#define OUTPUT_FILENAME "out"
bool run_slow = false;
bool early_exit = false;
bool early_crash = false;
bool early_sleep = false;
bool trickle_up = false;
bool trickle_down = false;
bool critical_section = false; // run most of the time in a critical section
bool report_fraction_done = true;
bool network_usage = false;
double cpu_time = 0, comp_result;
//returns the result of a to be b power using the binary representation of b
// Based orginally on code by Mark Lotts
long long fastExponent(long long a, long long b, long long prime) {
long long power, digit;
power = 1;
//for very large powers, this can be adjusted accordingly
digit = ((long long) 1 << 50);
while ((digit & b) == 0) {
digit = digit >> 1;
}
while (digit != 1) {
if (digit & b) {
power = (power * a) % prime;
}
power = (power * power) % prime;
digit = digit >> 1;
}
if (digit & b) {
power = (power * a) % prime;
}
return (power % prime);
}
long long slowExponent(long long a, long long b, long long prime) {
long long power, i;
power = 1;
for (i = 1; i < b + 1; i++) {
power = (power * a) % prime;
}
return (power % prime);
}
//finds the least primitive root of parameter prime
// Based orginally on code by Mark Lotts
long long getFactorRoot(long long prime) {
long long primefactors[256]; // number of prime factors is less than number of bits
long long guess, curNumber, i, foundone;
long long factorcount, curPrime, factordown;
factordown = (prime - 1);
factorcount = 0;
for (curPrime = 2;; curPrime++) {
bool isPrime = true;
for (long long possibleFactor = 2; possibleFactor < sqrt((long double)curPrime) + 1; possibleFactor++) {
if (curPrime % possibleFactor == 0 && curPrime != 2) {
isPrime = false;
break;
}
}
if (!isPrime)
continue;
if ((factordown % curPrime == 0)) {
primefactors[factorcount] = curPrime;
factorcount++;
while (factordown % curPrime == 0) {
factordown = factordown / curPrime;
}
}
if (factordown > 1 && curPrime > sqrt((long double)factordown) + 1) {
primefactors[factorcount] = factordown;
factorcount++;
factordown = 1;
}
if (factordown == 1)
break;
}
guess = 2;
while (true) {
foundone = 0;
for (i = 0; i < factorcount; ++i) {
curNumber = fastExponent(guess, ((prime - 1) / primefactors[i]), prime);
if (curNumber == 1) {
foundone = 1;
break;
}
}
if (foundone == 0) {
break;
}
guess++;
}
return guess;
}
// Code written by Daniel Monroe
int main(int argc, char **argv) {
int i;
int retval;
double fsize;
char input_path[512], output_path[512], buf[256];
MFILE out;
FILE* infile;
for (i=0; i<argc; i++) {
if (strstr(argv[i], "early_exit")) early_exit = true;
if (strstr(argv[i], "early_crash")) early_crash = true;
if (strstr(argv[i], "early_sleep")) early_sleep = true;
if (strstr(argv[i], "run_slow")) run_slow = true;
if (strstr(argv[i], "critical_section")) critical_section = true;
if (strstr(argv[i], "network_usage")) network_usage = true;
if (strstr(argv[i], "cpu_time")) {
cpu_time = atof(argv[++i]);
}
if (strstr(argv[i], "trickle_up")) trickle_up = true;
if (strstr(argv[i], "trickle_down")) trickle_down = true;
}
retval = boinc_init();
if (retval) {
fprintf(stderr, "%s boinc_init returned %d\n",
boinc_msg_prefix(buf, sizeof(buf)), retval
);
exit(retval);
}
boinc_resolve_filename(INPUT_FILENAME, input_path, sizeof(input_path));
long long thisPrime=0;
long long interval =1;
ifstream inputfile(input_path);
if (inputfile.is_open()) {
if (inputfile) {
string s;
if (getline(inputfile, s)) {
istringstream ss(s);
if(getline(ss, s, ',')) {
thisPrime = atoi(s.c_str());
if(getline(ss, s, ',')) {
interval = atoi(s.c_str());
}
}
}
}
inputfile.close();
}
if(interval==0) interval=1;
infile = boinc_fopen(input_path, "r");
if (!infile) {
fprintf(stderr,
"%s Couldn't find input file, resolved name %s.\n",
boinc_msg_prefix(buf, sizeof(buf)), input_path
);
exit(-1);
}
file_size(input_path, fsize);
int colors = 11;
long long bestPrimes[100][13];
for (i = 0; i < 100; i++)
for (int numberofcolors = 0; numberofcolors < colors; numberofcolors++)
bestPrimes[i][colors] = 0;
for (long long possiblePrime = thisPrime; possiblePrime <thisPrime+interval; possiblePrime++) {
bool isPrime = true;
for (long long possibleFactor = 2; possibleFactor*possibleFactor < possiblePrime+2; possibleFactor++) {
if (possiblePrime % possibleFactor == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
long long* powers = new long long[(unsigned int) possiblePrime + 20];
long long possibleRoot = getFactorRoot(possiblePrime);
{
powers[possibleRoot] = 1;
long long accumulatedPower = possibleRoot;
bool isPrimitive = true;
for (long long exponent = 2; exponent <= possiblePrime - 1; exponent++) {
accumulatedPower = (possibleRoot * accumulatedPower) % possiblePrime;
if ((accumulatedPower == 1) && (exponent < possiblePrime - 1)) {
isPrimitive = false;
break;
}
powers[accumulatedPower] = exponent;
}
if (!isPrimitive) {
return 0;
}
if (isPrimitive) {
long long* finalLength = new long long[(unsigned int) colors + 1];
long long* currentLength = new long long[(unsigned int) colors + 1];
long long* sequenceFromOne = new long long[(unsigned int) colors + 1];
for (int numberofcolors = 0; numberofcolors < colors; numberofcolors++) {
currentLength[numberofcolors] = 1;
finalLength[numberofcolors] = 0;
sequenceFromOne[numberofcolors] = 1;
}
int numberofcolors = 2;
for (long long position = 2; position <= possiblePrime - 1; position++) {
for (numberofcolors = 2; numberofcolors < colors; numberofcolors++) {
if ((powers[position] % numberofcolors) == (powers[position - 1] % numberofcolors)) {
currentLength[numberofcolors]++;
} else {
if (finalLength[numberofcolors] == 0)
sequenceFromOne[numberofcolors] = currentLength[numberofcolors];
finalLength[numberofcolors] = max(finalLength[numberofcolors], currentLength[numberofcolors]);
currentLength[numberofcolors] = 1;
}
}
}
for (numberofcolors = 2; numberofcolors < colors; numberofcolors++) {
if ((possiblePrime % numberofcolors) != 1)
continue;
long long length = 0;
length = finalLength[numberofcolors] + 1;
if ((powers[possiblePrime - 1] % numberofcolors) == 0) {
length = max(finalLength[numberofcolors] + 1, sequenceFromOne[numberofcolors] * 2 + 2);
} else {
length = max(finalLength[numberofcolors] + 1, sequenceFromOne[numberofcolors] + 2);
}
if (length < 24 && possiblePrime > bestPrimes[length][numberofcolors]) {
bestPrimes[length][numberofcolors] = possiblePrime;
}
}
delete[] finalLength;
finalLength = NULL;
delete[] currentLength;
currentLength = NULL;
delete[] sequenceFromOne;
sequenceFromOne = NULL;
}
}
delete[] powers;
powers = NULL;
}
}
boinc_resolve_filename(OUTPUT_FILENAME, output_path, sizeof(output_path));
ofstream outputfile(output_path);
if (outputfile.is_open()) {
if (outputfile.is_open()) {
outputfile << thisPrime<<","<<interval << endl;
//outputfile << "test" << endl;
fprintf(stderr,"%llu,%llu\n", thisPrime,interval);
}
for ( i = 3; i < 24; i++) {
for (int numberofcolors = 2; numberofcolors < colors; numberofcolors++) {
outputfile << bestPrimes[i][numberofcolors];
outputfile << ",";
fprintf(stderr,"%llu,",bestPrimes[i][numberofcolors]);
}
outputfile << endl;
fprintf(stderr, "\n");
}
}
outputfile.close();
boinc_fraction_done(1);
boinc_finish(0);
}
#ifdef _WIN32
int WINAPI WinMain(
HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode
) {
LPSTR command_line;
char* argv[100];
int argc;
command_line = GetCommandLine();
argc = parse_command_line(command_line, argv);
return main(argc, argv);
}
#endif