-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindFactors.cs
786 lines (621 loc) · 23.3 KB
/
FindFactors.cs
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
// Programming by Eric Chauvin.
// Notes on this source code are at:
// ericbreakingrsa.blogspot.com
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel; // BackgroundWorker
namespace ExampleServer
{
internal struct OneFactorRec
{
internal Integer Factor;
internal bool IsDefinitelyAPrime;
internal bool IsDefinitelyNotAPrime;
}
class FindFactors
{
private IntegerMath IntMath;
private Integer Quotient;
private Integer Remainder;
private BackgroundWorker Worker;
private uint[] DivisionArray;
private uint[] QuadResArray;
private uint QuadResArrayLast = 0;
private uint QuadResBigBase = 0;
private OneFactorRec[] FactorsArray;
private int FactorsArrayLast = 0;
private int[] SortIndexArray;
private Integer OriginalFindFrom;
private Integer FindFrom;
private SortedDictionary<uint, uint> StatsDictionary;
private int NumbersTested = 0;
private FindFactors()
{
}
internal FindFactors( BackgroundWorker UseWorker, IntegerMath UseIntMath )
{
Worker = UseWorker;
IntMath = UseIntMath;
Quotient = new Integer();
Remainder = new Integer();
OriginalFindFrom = new Integer();
FindFrom = new Integer();
FactorsArray = new OneFactorRec[8];
SortIndexArray = new int[8];
StatsDictionary = new SortedDictionary<uint, uint>();
}
private void AddFactorRec( OneFactorRec Rec )
{
// if( Rec == null )
// return false;
FactorsArray[FactorsArrayLast] = Rec;
SortIndexArray[FactorsArrayLast] = FactorsArrayLast;
FactorsArrayLast++;
if( FactorsArrayLast >= FactorsArray.Length )
{
try
{
Array.Resize( ref FactorsArray, FactorsArray.Length + 16 );
Array.Resize( ref SortIndexArray, FactorsArray.Length );
}
catch( Exception Except )
{
throw( new Exception( "Couldn't resize the arrays for FindFactors.cs. " + Except.Message ));
}
}
}
private void ClearFactorsArray()
{
for( int Count = 0; Count < FactorsArrayLast; Count++ )
FactorsArray[Count].Factor = null;
FactorsArrayLast = 0;
// Don't resize the array.
}
internal void ShowStats()
{
Worker.ReportProgress( 0, " " );
Worker.ReportProgress( 0, "Stats NumbersTested: " + NumbersTested.ToString() );
uint Count = 0;
ulong Total = 0;
uint TotalPrimes = 0;
foreach( KeyValuePair<uint, uint> Kvp in StatsDictionary )
{
Count++;
TotalPrimes += Kvp.Value; // Value is how many times it found a number.
Total += (Kvp.Key * Kvp.Value); // The prime times how many times it found it.
}
if( Count > 0 )
{
ulong Average = Total / Count;
Worker.ReportProgress( 0, "Number of primes found: " + TotalPrimes.ToString( "N0" ) );
Worker.ReportProgress( 0, "Total: " + Total.ToString( "N0" ) );
Worker.ReportProgress( 0, "Average: " + Average.ToString( "N0" ) );
}
foreach( KeyValuePair<uint, uint> Kvp in StatsDictionary )
Worker.ReportProgress( 0, Kvp.Key.ToString( "N0" ) + "\t" + Kvp.Value.ToString( "N0" ) );
}
private void AddToStats( uint Prime )
{
if( StatsDictionary.ContainsKey( Prime ))
StatsDictionary[Prime] = StatsDictionary[Prime] + 1;
else
StatsDictionary[Prime] = 1;
}
internal void FindSmallPrimeFactorsOnly( Integer FindFromNotChanged )
{
OriginalFindFrom.Copy( FindFromNotChanged );
FindFrom.Copy( FindFromNotChanged );
ClearFactorsArray();
Integer OneFactor;
OneFactorRec Rec;
// while( not forever )
for( int Count = 0; Count < 1000; Count++ )
{
if( Worker.CancellationPending )
return;
uint SmallPrime = IntMath.IsDivisibleBySmallPrime( FindFrom );
if( SmallPrime == 0 )
break; // No more small primes.
// Worker.ReportProgress( 0, "Found a small prime factor: " + SmallPrime.ToString() );
OneFactor = new Integer();
OneFactor.SetFromULong( SmallPrime );
Rec = new OneFactorRec();
Rec.Factor = OneFactor;
Rec.IsDefinitelyAPrime = true;
AddFactorRec( Rec );
if( FindFrom.IsULong())
{
ulong CheckLast = FindFrom.GetAsULong();
if( CheckLast == SmallPrime )
{
// Worker.ReportProgress( 0, "It only had small prime factors." );
VerifyFactors();
return; // It had only small prime factors.
}
}
IntMath.Divide( FindFrom, OneFactor, Quotient, Remainder );
if( !Remainder.IsZero())
throw( new Exception( "Bug in FindSmallPrimeFactorsOnly(). Remainder is not zero." ));
FindFrom.Copy( Quotient );
if( FindFrom.IsOne())
throw( new Exception( "Bug in FindSmallPrimeFactorsOnly(). This was already checked for 1." ));
}
// Worker.ReportProgress( 0, "One factor was not a small prime." );
OneFactor = new Integer();
OneFactor.Copy( FindFrom );
Rec = new OneFactorRec();
Rec.Factor = OneFactor;
AddFactorRec( Rec );
// Worker.ReportProgress( 0, "No more small primes." );
}
internal void FindAllFactors( Integer FindFromNotChanged )
{
// ShowStats(); // So far.
OriginalFindFrom.Copy( FindFromNotChanged );
FindFrom.Copy( FindFromNotChanged );
NumbersTested++;
ClearFactorsArray();
Integer OneFactor;
OneFactorRec Rec;
// while( not forever )
for( int Count = 0; Count < 1000; Count++ )
{
if( Worker.CancellationPending )
return;
uint SmallPrime = IntMath.IsDivisibleBySmallPrime( FindFrom );
if( SmallPrime == 0 )
break; // No more small primes.
// Worker.ReportProgress( 0, "Found a small prime factor: " + SmallPrime.ToString() );
AddToStats( SmallPrime );
OneFactor = new Integer();
OneFactor.SetFromULong( SmallPrime );
Rec = new OneFactorRec();
Rec.Factor = OneFactor;
Rec.IsDefinitelyAPrime = true;
AddFactorRec( Rec );
if( FindFrom.IsULong())
{
ulong CheckLast = FindFrom.GetAsULong();
if( CheckLast == SmallPrime )
{
Worker.ReportProgress( 0, "It only had small prime factors." );
VerifyFactors();
return; // It had only small prime factors.
}
}
IntMath.Divide( FindFrom, OneFactor, Quotient, Remainder );
if( !Remainder.IsZero())
throw( new Exception( "Bug in FindAllFactors. Remainder is not zero." ));
FindFrom.Copy( Quotient );
if( FindFrom.IsOne())
throw( new Exception( "Bug in FindAllFactors. This was already checked for 1." ));
}
// Worker.ReportProgress( 0, "No more small primes." );
if( IsFermatPrimeAdded( FindFrom ))
{
VerifyFactors();
return;
}
// while( not forever )
for( int Count = 0; Count < 1000; Count++ )
{
if( Worker.CancellationPending )
return;
// If FindFrom is a ulong then this will go up to the square root of
// FindFrom and return zero if it doesn't find it there. So it can't
// go up to the whole value of FindFrom.
uint SmallFactor = NumberIsDivisibleByUInt( FindFrom );
if( SmallFactor == 0 )
break;
// This is necessarily a prime because it was the smallest one found.
AddToStats( SmallFactor );
// Worker.ReportProgress( 0, "Found a small factor: " + SmallFactor.ToString( "N0" ));
OneFactor = new Integer();
OneFactor.SetFromULong( SmallFactor );
Rec = new OneFactorRec();
Rec.Factor = OneFactor;
Rec.IsDefinitelyAPrime = true; // The smallest factor. It is necessarily a prime.
AddFactorRec( Rec );
IntMath.Divide( FindFrom, OneFactor, Quotient, Remainder );
if( !Remainder.IsZero())
throw( new Exception( "Bug in FindAllFactors. Remainder is not zero. Second part." ));
if( Quotient.IsOne())
throw( new Exception( "This can't happen here. It can't go that high." ));
FindFrom.Copy( Quotient );
if( IsFermatPrimeAdded( FindFrom ))
{
VerifyFactors();
return;
}
}
if( IsFermatPrimeAdded( FindFrom ))
{
VerifyFactors();
return;
}
// If it got this far then it's definitely composite or definitely
// small enough to factor.
Integer P = new Integer();
Integer Q = new Integer();
bool TestedAllTheWay = FindTwoFactorsWithFermat( FindFrom, P, Q, 0 );
if( !P.IsZero())
{
// Q is necessarily prime because it's bigger than the square root.
// But P is not necessarily prime.
// P is the smaller one, so add it first.
if( IsFermatPrimeAdded( P ))
{
Worker.ReportProgress( 0, "P from Fermat method was probably a prime." );
}
else
{
OneFactor = new Integer();
OneFactor.Copy( P );
Rec = new OneFactorRec();
Rec.Factor = OneFactor;
Rec.IsDefinitelyNotAPrime = true;
AddFactorRec( Rec );
}
Worker.ReportProgress( 0, "Q is necessarily prime." );
OneFactor = new Integer();
OneFactor.Copy( Q );
Rec = new OneFactorRec();
Rec.Factor = OneFactor;
Rec.IsDefinitelyAPrime = true;
AddFactorRec( Rec );
}
else
{
// Didn't find any with Fermat.
OneFactor = new Integer();
OneFactor.Copy( FindFrom );
Rec = new OneFactorRec();
Rec.Factor = OneFactor;
if( TestedAllTheWay )
Rec.IsDefinitelyAPrime = true;
else
Rec.IsDefinitelyNotAPrime = true;
AddFactorRec( Rec );
}
Worker.ReportProgress( 0, "That's all it could find." );
VerifyFactors();
}
private bool IsFermatPrimeAdded( Integer FindFrom )
{
if( FindFrom.IsULong())
{
// The biggest size that NumberIsDivisibleByUInt() will check to
// see if it has primes for sure.
if( FindFrom.GetAsULong() < (223092870UL * 223092870UL))
return false; // Factor this.
}
int HowManyTimes = 20; // How many primes it will be checked with.
if( !IntMath.IsFermatPrime( FindFrom, HowManyTimes ))
return false;
Integer OneFactor = new Integer();
OneFactor.Copy( FindFrom );
OneFactorRec Rec = new OneFactorRec();
Rec.Factor = OneFactor;
// Neither one of these is set to true here because it's probably
// a prime, but not definitely.
// Rec.IsDefinitelyAPrime = false;
// Rec.IsDefinitelyNotAPrime = false;
AddFactorRec( Rec );
Worker.ReportProgress( 0, "Fermat thinks this one is a prime." );
return true; // It's a Fermat prime and it was added.
}
internal void ShowAllFactors()
{
Worker.ReportProgress( 0, " " );
Worker.ReportProgress( 0, "Factors:" );
for( int Count = 0; Count < FactorsArrayLast; Count++ )
{
Worker.ReportProgress( 0, IntMath.ToString10( FactorsArray[Count].Factor ) );
if( FactorsArray[Count].IsDefinitelyAPrime )
Worker.ReportProgress( 0, "Is a prime." );
if( FactorsArray[Count].IsDefinitelyNotAPrime )
Worker.ReportProgress( 0, "Is not a prime." );
if( !(FactorsArray[Count].IsDefinitelyAPrime || FactorsArray[Count].IsDefinitelyNotAPrime ))
Worker.ReportProgress( 0, "It's likely to be a prime, but it might not be." );
}
Worker.ReportProgress( 0, " " );
}
internal void VerifyFactors()
{
Integer TestFactors = new Integer();
TestFactors.SetToOne();
for( int Count = 0; Count < FactorsArrayLast; Count++ )
IntMath.Multiply( TestFactors, FactorsArray[Count].Factor );
if( !TestFactors.IsEqual( OriginalFindFrom ))
throw( new Exception( "VerifyFactors didn't come out right." ));
}
private void SetupDivisionArray()
{
// If you were going to try and find the prime factors of a number,
// the most basic way would be to divide it by every prime up to the
// prime that finally divides it evenly. The problem with doing that
// is that it takes longer to figure out if a number is a prime than
// it does to just test a lot of numbers that are less likely to be
// composite than other numbers. Any table of primes like the one
// in IntegerMath.PrimeArray would only last for a split second, then
// you'd have to go on to some other method for bigger numbers.
// If you pick a number at random, then statistically, half of all
// numbers are odd, a third of all numbers are divisible by 3, a
// fifth of all numbers are divisible by 5, a seventh of all numbers
// are divisible by 7, and so on. So you can reduce the amount of
// numbers that you test with by getting rid of those numbers that
// are divisible by small primes.
// The Euler Phi function shows the number of numbers that are relatively
// prime to some other number. So it gives you the size of the array
// for these numbers. It is (2 - 1)(3 - 1)(5 - 1)... and so on.
uint Base = 2 * 3 * 5 * 7 * 11 * 13 * 17;
uint EulerPhi = 2 * 4 * 6 * 10 * 12 * 16;
DivisionArray = new uint[EulerPhi];
// The first few numbers in this array are:
// 1, 19, 23, 29, 31, 37, 41 ... and so on.
int Index = 0;
for( uint Count = 0; Count < Base; Count++ )
{
if( (Count & 1) == 0 ) // If its an even number.
continue;
if( (Count % 3) == 0 ) // If its divisible by 3.
continue;
if( (Count % 5) == 0 )
continue;
if( (Count % 7) == 0 )
continue;
if( (Count % 11) == 0 )
continue;
if( (Count % 13) == 0 )
continue;
if( (Count % 17) == 0 )
continue;
DivisionArray[Index] = Count;
Index++;
}
}
internal uint NumberIsDivisibleByUInt( Integer ToCheck )
{
if( DivisionArray == null )
SetupDivisionArray(); // Set it up once, when it's needed.
uint Max = 0;
if( ToCheck.IsULong())
{
ulong ForMax = ToCheck.GetAsULong();
// It can't be bigger than the square root.
Max = (uint)IntMath.FindULSqrRoot( ForMax );
}
uint Base = 2 * 3 * 5 * 7 * 11 * 13 * 17;
uint EulerPhi = 2 * 4 * 6 * 10 * 12 * 16;
uint Base19 = Base * 19;
uint Base23 = Base19 * 23;
// The first few base numbers like this:
// 2 2
// 3 6
// 5 30
// 7 210
// 11 2,310
// 13 30,030
// 17 510,510
// 19 9,699,690
// 23 223,092,870
// These loops count up to 223,092,870 - 1.
for( uint Count23 = 0; Count23 < 23; Count23++ )
{
Worker.ReportProgress( 0, "Count23 loop: " + Count23.ToString());
uint Base23Part = (Base19 * Count23);
for( uint Count19 = 0; Count19 < 19; Count19++ )
{
uint Base19Part = Base * Count19;
if( Worker.CancellationPending )
return 0;
for( int Count = 0; Count < EulerPhi; Count++ )
{
if( Worker.CancellationPending )
return 0;
uint Test = Base23Part + Base19Part + DivisionArray[Count];
if( Test == 1 )
continue;
if( (Test % 19) == 0 )
continue;
if( (Test % 23) == 0 )
continue;
if( Max > 0 )
{
if( Test > Max )
return 0;
}
if( 0 == IntMath.GetMod32( ToCheck, Test ))
{
Worker.ReportProgress( 0, "The number is divisible by: " + Test.ToString( "N0" ));
return Test;
}
}
}
}
return 0; // Didn't find a number to divide it.
}
private void SetupQuadResArray( Integer Product )
{
// I'm doing this differently from finding y^2 = x^2 - N,
// which I think would be faster, unless it complicates it too
// much by having to use large Integers and doing subtraction.
// Here it's looking for when
// P + x^2 = y^2.
// private uint[] QuadResArraySmall;
// private uint[] QuadResArray;
uint SmallBase = 2 * 3 * 5 * 7 * 11 * 13; // 30,030
// 2 3 3 4 6 7
int SmallBaseArraySize = 2 * 3 * 3 * 4 * 6 * 7; // This is not exact.
uint[] QuadResArraySmall = new uint[SmallBaseArraySize];
uint QuadResArraySmallLast = 0;
uint ProductModSmall = (uint)IntMath.GetMod32( Product, SmallBase );
QuadResArraySmallLast = 0;
uint ProdMod4 = (uint)Product.GetD( 0 ) & 3;
for( ulong Count = 0; Count < SmallBase; Count++ )
{
// P is odd.
// if x is even then y is odd.
// if x is odd then y is even.
// If x is even then x^2 is divisible by 4.
// If y is even then y^2 is divisible by 4.
ulong Test = ProductModSmall + (Count * Count); // The Product plus a square.
Test = Test % SmallBase;
if( !IntegerMath.IsSmallQuadResidue( (uint)Test ))
continue;
// What Count was used to make a quad residue?
QuadResArraySmall[QuadResArraySmallLast] = (uint)Count;
QuadResArraySmallLast++;
if( QuadResArraySmallLast >= SmallBaseArraySize )
throw( new Exception( "Went past the small quad res array." ));
}
// Worker.ReportProgress( 0, "Finished setting up small quad res array." );
QuadResBigBase = SmallBase * 17 * 19 * 23; // 223,092,870
// 17 19 23
int QuadResBaseArraySize = SmallBaseArraySize * 9 * 10 * 12; // This is not exact.
QuadResArray = new uint[QuadResBaseArraySize];
uint ProductMod = (uint)IntMath.GetMod32( Product, QuadResBigBase );
int MaxLength = QuadResArray.Length;
QuadResArrayLast = 0;
for( ulong Count23 = 0; Count23 < (17 * 19 * 23); Count23++ )
{
if( Worker.CancellationPending )
return;
ulong BasePart = Count23 * SmallBase;
for( uint Count = 0; Count < QuadResArraySmallLast; Count++ )
{
ulong CountPart = BasePart + QuadResArraySmall[Count];
ulong Test = ProductMod + (CountPart * CountPart); // The Product plus a square.
Test = Test % QuadResBigBase;
if( !IntegerMath.IsQuadResidue17To23( (uint)Test ))
continue;
// What Count was used to make a quad residue?
QuadResArray[QuadResArrayLast] = (uint)CountPart;
QuadResArrayLast++;
if( QuadResArrayLast >= MaxLength )
throw( new Exception( "Went past the quad res array." ));
}
}
Worker.ReportProgress( 0, "Finished setting up main quad res array." );
}
internal bool FindTwoFactorsWithFermat( Integer Product, Integer P, Integer Q, ulong MinimumX )
{
ECTime StartTime = new ECTime();
StartTime.SetToNow();
Integer TestSqrt = new Integer();
Integer TestSquared = new Integer();
Integer SqrRoot = new Integer();
TestSquared.Copy( Product );
IntMath.Multiply( TestSquared, Product );
IntMath.SquareRoot( TestSquared, SqrRoot );
TestSqrt.Copy( SqrRoot );
IntMath.DoSquare( TestSqrt );
// IntMath.Multiply( TestSqrt, SqrRoot );
if( !TestSqrt.IsEqual( TestSquared ))
throw( new Exception( "The square test was bad." ));
// Some primes:
// 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
// 101, 103, 107
P.SetToZero();
Q.SetToZero();
Integer TestX = new Integer();
SetupQuadResArray( Product );
ulong BaseTo37 = QuadResBigBase * 29UL * 31UL * 37UL;
// ulong BaseTo31 = QuadResBigBase * 29UL * 31UL;
ulong ProdModTo37 = IntMath.GetMod64( Product, BaseTo37 );
// ulong ProdModTo31 = IntMath.GetMod64( Product, BaseTo31 );
for( ulong BaseCount = 0; BaseCount < (29 * 31 * 37); BaseCount++ )
{
if( (BaseCount & 0xF) == 0 )
Worker.ReportProgress( 0, "Find with Fermat BaseCount: " + BaseCount.ToString() );
if( Worker.CancellationPending )
return false;
ulong Base = (BaseCount + 1) * QuadResBigBase; // BaseCount times 223,092,870.
if( Base < MinimumX )
continue;
Base = BaseCount * QuadResBigBase; // BaseCount times 223,092,870.
for( uint Count = 0; Count < QuadResArrayLast; Count++ )
{
// The maximum CountPart can be is just under half the size of
// the Product. (Like if Y - X was equal to 1, and Y + X was
// equal to the Product.) If it got anywhere near that big it
// would be inefficient to try and find it this way.
ulong CountPart = Base + QuadResArray[Count];
ulong Test = ProdModTo37 + (CountPart * CountPart);
// ulong Test = ProdModTo31 + (CountPart * CountPart);
Test = Test % BaseTo37;
// Test = Test % BaseTo31;
if( !IntegerMath.IsQuadResidue29( Test ))
continue;
if( !IntegerMath.IsQuadResidue31( Test ))
continue;
if( !IntegerMath.IsQuadResidue37( Test ))
continue;
ulong TestBytes = (CountPart & 0xFFFFF);
TestBytes *= (CountPart & 0xFFFFF);
ulong ProdBytes = Product.GetD( 1 );
ProdBytes <<= 8;
ProdBytes |= Product.GetD( 0 );
uint FirstBytes = (uint)(TestBytes + ProdBytes);
if( !IntegerMath.FirstBytesAreQuadRes( FirstBytes ))
{
// Worker.ReportProgress( 0, "First bytes aren't quad res." );
continue;
}
TestX.SetFromULong( CountPart );
IntMath.MultiplyULong( TestX, CountPart );
TestX.Add( Product );
// uint Mod37 = (uint)IntMath.GetMod32( TestX, 37 );
// if( !IntegerMath.IsQuadResidue37( Mod37 ))
// continue;
// Do more of these tests with 41, 43, 47...
// if( !IntegerMath.IsQuadResidue41( Mod37 ))
// continue;
// Avoid doing this square root at all costs.
if( IntMath.SquareRoot( TestX, SqrRoot ))
{
Worker.ReportProgress( 0, " " );
if( (CountPart & 1) == 0 )
Worker.ReportProgress( 0, "CountPart was even." );
else
Worker.ReportProgress( 0, "CountPart was odd." );
// Found an exact square root.
// P + (CountPart * CountPart) = Y*Y
// P = (Y + CountPart)Y - CountPart)
P.Copy( SqrRoot );
Integer ForSub = new Integer();
ForSub.SetFromULong( CountPart );
IntMath.Subtract( P, ForSub );
// Make Q the bigger one and put them in order.
Q.Copy( SqrRoot );
Q.AddULong( CountPart );
if( P.IsOne() || Q.IsOne())
{
// This happens when testing with small primes.
Worker.ReportProgress( 0, " " );
Worker.ReportProgress( 0, " " );
Worker.ReportProgress( 0, "Went all the way to 1 in FindTwoFactorsWithFermat()." );
Worker.ReportProgress( 0, " " );
Worker.ReportProgress( 0, " " );
P.SetToZero(); // It has no factors.
Q.SetToZero();
return true; // Tested everything, so it's a prime.
}
Worker.ReportProgress( 0, "Found P: " + IntMath.ToString10( P ) );
Worker.ReportProgress( 0, "Found Q: " + IntMath.ToString10( Q ) );
Worker.ReportProgress( 0, "Seconds: " + StartTime.GetSecondsToNow().ToString( "N1" ));
Worker.ReportProgress( 0, " " );
throw( new Exception( "Testing this." ));
// return true; // With P and Q.
}
// else
// Worker.ReportProgress( 0, "It was not an exact square root." );
}
}
// P and Q would still be zero if it never found them.
return false;
}
}
}