-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
418 lines (352 loc) · 13.9 KB
/
Form1.cs
File metadata and controls
418 lines (352 loc) · 13.9 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
namespace SimpleNumbers
{
public partial class Form1 : Form
{
string fileName = "simples.txt";
string binName = "simples.bin";
string binNameD = "simplesDict.bin";
string fMults = "nosimples.txt";
public Form1()
{
InitializeComponent();
}
[DllImport("Kernel32.dll")]
static extern Int32 CreateFile(string lpFileName, UInt32 dwDesiredAccess, Int32 dwShareMode, Int32 lpSecurityAttributes,
Int32 dwCreationDisposition, Int32 dwFlagsAndAttributes, Int32 hTemplateFile);
[DllImport("Kernel32.dll")]
static extern Int32 SetFilePointerEx(Int32 hFile, long liDistanceToMove, out long lpNewFilePointer, int dwMoveMethod);
[DllImport("Kernel32.dll")]
static unsafe extern Int32 ReadFile(Int32 hFile, byte* buffer, int nNumberOfBytesToRead, out int bytesReaded, int lpOverlapped);
[DllImport("Kernel32.dll")]
static unsafe extern Int32 ReadFile(Int32 hFile, byte[] buffer, int nNumberOfBytesToRead, out int bytesReaded, int lpOverlapped);
[DllImport("Kernel32.dll")]
static unsafe extern Int32 WriteFile(Int32 hFile, byte[] buffer, int nNumberOfBytesToWrite, out int NumberOfBytesWritten, int lpOverlapped);
[DllImport("Kernel32.dll")]
static unsafe extern Int32 WriteFile(Int32 hFile, byte* buffer, int nNumberOfBytesToWrite, out int NumberOfBytesWritten, int lpOverlapped);
[DllImport("Kernel32.dll")]
static extern Int32 CloseHandle(Int32 lpBaseAddress);
[DllImport("Kernel32.dll")]
static extern Int32 GetLastError();
bool terminate = true;
private void button1_Click(object sender, EventArgs e)
{
if (terminate == true)
{
terminate = false;
getSimples();
}
else
terminate = true;
}
public unsafe static void ULongToBytes(ulong data, ref byte[] target, long start = 0)
{
if (target == null)
target = new byte[8];
if (start < 0 || start + 8 > target.LongLength)
throw new IndexOutOfRangeException();
fixed (byte * t = target)
{
for (long i = start; i < start + 8; i++)
{
*(t + i) = (byte) data;
data = data >> 8;
}
}
}
public unsafe static void BytesToULong(out ulong data, byte[] target, long start = 0)
{
data = 0;
if (start < 0 || start + 8 > target.LongLength)
throw new IndexOutOfRangeException();
fixed (byte * t = target)
{
for (long i = start + 8 - 1; i >= start; i--)
{
data <<= 8;
data += *(t + i);
}
}
}
/*
byte[] _n1 = new byte[8], _n2 = new byte[8];
int tmp; long tmpl;
private unsafe void getSimples()
{
if (!File.Exists(binName) || !File.Exists(binNameD))
{
ULongToBytes(3, ref _n1);
File.WriteAllBytes(binName, _n1);
ULongToBytes(0, ref _n1);
File.WriteAllBytes(binNameD, _n1);
File.WriteAllText(fileName, "2\r\n3\r\n");
File.WriteAllText(fMults, "\r\n\r\n3\r\n\r\n");
}
int bin = 0, bind = 0;
try
{
ulong currentNumber = 3;
ulong toAdd = 2;
bool isNotFound = false;
ulong countOfSimples = 1;
long len;*/
// bin = CreateFile(binName, 0xC0000000, 0/*0x00000001 | 0x00000002 */ , 0, 3, 0x80 | 0x08000000 /* последовательное сканирование */ /*| 0x20000000*/ /* без буферизации */, 0);
/*bin = CreateFile(binName, 0xC0000000, 0, 0, 3, 0x80, 0);
bind = CreateFile(binNameD, 0xC0000000, 0, 0, 3, 0x80, 0);
SetFilePointerEx(bin, 0, out len, 2);
SetFilePointerEx(bin, -8, out tmpl, 2);
ReadFile(bin, _n1, 8, out tmp, 0);
BytesToULong(out currentNumber, _n1);
label2.Text = currentNumber.ToString("0 000");
label1.Text = countOfSimples.ToString("0 000");
Application.DoEvents();
while (!terminate)
{
currentNumber += 2;
isNotFound = false;
for (long i = 0; i < len; i += 8)
{
ulong currentSimple, currentMod;
getModNumber(i, out currentSimple, out currentMod, bin, bind);
currentMod = currentNumber % currentSimple;
if (currentMod == 0)
{
isNotFound = true;
#if multi
File.AppendAllText(fMults, currentSimple + " ");
#else
break;
#endif
}
}
if (isNotFound)
{
toAdd += 2;
#if multi
File.AppendAllText(fMults, "\r\n\r\n");
#endif
continue;
}
#if multi
File.AppendAllText(fMults, currentNumber + "\r\n\r\n");
#endif
// нашли простое число
countOfSimples++;
File.AppendAllText(fileName, currentNumber + "\r\n");
for (long i = 0; i < len; i += 8)
{
ulong currentSimple, currentMod;
getModNumber(i, out currentSimple, out currentMod, bin, bind);
currentMod += toAdd;
if (currentMod >= currentSimple)
currentMod = currentMod % currentSimple;
// setModNumber(i, currentMod, bind, len);
}
ULongToBytes(currentNumber, ref _n1);
ULongToBytes(0, ref _n2);
SetFilePointerEx(bin, 0, out len, 2);
SetFilePointerEx(bind, 0, out len, 2);
fixed (byte* n1 = _n1, n2 = _n2)
{
WriteFile(bin, n1, 8, out tmp, 0);
WriteFile(bind, n2, 8, out tmp, 0);
}
len += 8;
toAdd = 2;
label2.Text = currentNumber.ToString("000 000 000 000 000 000 000 000");
label1.Text = countOfSimples.ToString("000 000 000 000 000 000 000 000");
Application.DoEvents();
}
}
finally
{
CloseHandle(bin);
CloseHandle(bind);
}
}
const long constLen = 1024*1024*128;
ulong[] caches = new ulong[constLen];
ulong[] cached = new ulong[constLen];
private unsafe void getModNumber(long i, out ulong currentSimple, out ulong currentMod, int bin, int bind)
{
var t = i >> 3;
if (t < constLen && caches[t] > 0)
{
currentSimple = caches[t];
currentMod = 0;//cached[t];
}
else
{
SetFilePointerEx(bin, i, out tmpl, 0);
//SetFilePointerEx(bind, i, out tmpl, 0);
fixed (byte* n1 = _n1)
{
ReadFile(bin, n1, 8, out tmp, 0);
}
BytesToULong(out currentSimple, _n1);
//BytesToULong(out currentMod, _n2);
currentMod = 0;
caches[t] = currentSimple;
//cached[t] = currentMod;
}
}
int lastFlush = 1000;
private unsafe void setModNumber(long i, ulong currentMod, int bind, long len)
{
if ((len >> 3) >= constLen || lastFlush-- < 0)
{
lastFlush = 1000;
SetFilePointerEx(bind, i, out tmpl, 0);
ULongToBytes(currentMod, ref _n2);
fixed (byte* n2 = _n2)
WriteFile(bind, n2, 8, out tmp, 0);
}
cached[i >> 3] = currentMod;
}
*/
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
terminate = true;
}
public unsafe static void IntToBytes(int data, ref byte[] target, long start = 0)
{
if (target == null)
target = new byte[4];
if (start < 0 || start + 4 > target.LongLength)
throw new IndexOutOfRangeException();
fixed (byte * t = target)
{
for (long i = start; i < start + 4; i++)
{
*(t + i) = (byte) data;
data = data >> 8;
}
}
}
public unsafe static void BytesToInt(out int data, byte[] target, long start = 0)
{
data = 0;
if (start < 0 || start + 4 > target.LongLength)
throw new IndexOutOfRangeException();
fixed (byte * t = target)
{
for (long i = start + 4 - 1; i >= start; i--)
{
data <<= 8;
data += *(t + i);
}
}
}
byte[] n1 = new byte[8];
int tmp;
byte[] simplies = new byte[1024L*1024L*1024L];
private unsafe void getSimples()
{
File.WriteAllText(fileName, "");
File.WriteAllText(binName, "");
label2.Text = "Подготавливается";
label1.Text = "";
Application.DoEvents();
ulong countOfSimples = 1;
for (long i = 0; i < simplies.LongLength; i++)
simplies[i] = 0;
label2.Text = "0";
label1.Text = "";
Application.DoEvents();
if (terminate)
return;
int bin = CreateFile(binName, 0xC0000000, 0, 0, 3, 0x80 | 0x08000000, 0);
int str = CreateFile(fileName, 0xC0000000, 0x00000001 | 0x00000002, 0, 3, 0x80 | 0x08000000, 0);
if (bin <= 0 || str <= 0)
{
CloseHandle(str);
CloseHandle(bin);
label2.Text = "Ошибка при открытии файла";
return;
}
var nstr = Encoding.GetEncoding(1251).GetBytes("2\r\n");
WriteFile(str, nstr, nstr.Length, out tmp, 0);
ULongToBytes((ulong) 2, ref n1);
WriteFile(bin, n1, 4, out tmp, 0);
try
{
long len = simplies.LongLength/* << 3*/;
fixed (byte* s = simplies)
{
long a = 3;
for (long i = 0; i < len; i++, a += 2)
{
if (s[i] != 1)
{
// File.AppendAllText(fileName, "" + a + "\r\n");
//ULongToBytes((ulong) a, ref n1);
IntToBytes((int) a, ref n1);
WriteFile(bin, n1, 4, out tmp, 0);
if (tmp != 4)
throw new Exception();
// if (countOfSimples < 65535)
{
nstr = Encoding.GetEncoding(1251).GetBytes("" + a + "\r\n");
WriteFile(str, nstr, nstr.Length, out tmp, 0);
}
countOfSimples++;
if ((countOfSimples & 1023) == 0)
{
label2.Text = a.ToString("000 000 000 000 000 000 000 000");
label1.Text = countOfSimples.ToString("000 000 000 000 000 000 000 000");
Application.DoEvents();
if (terminate)
break;
}
}
else
continue;
for (long j = i + a; j < len; j += a)
{
// setBit(j, s);
s[j] = 1;
}
}
}
}
finally
{
CloseHandle(bin);
CloseHandle(str);
}
}
/*
private unsafe void setBit(long j, byte* s)
{
int k = (int) (j & 0x07);
long l = j >> 3;
byte c = (byte) (1 << k);
s[l] = (byte) (s[l] | c);
}
private unsafe bool getBit(long j, byte* s)
{
int k = (int) (j & 0x07);
long l = j >> 3;
byte c = (byte) (1 << k);
return (s[l] & c) > 0;
}
*/
private unsafe void setBit(long j, byte* s)
{
s[j] = 1;
}
private unsafe bool getBit(long j, byte* s)
{
return s[j] > 0;
}
}
}