-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcyclop.cpp
1150 lines (906 loc) · 31 KB
/
cyclop.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
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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This source distribution is placed into the public domain by its author, Jonathan Crombie.
* You may use it for whatever purpose, without charge, and without having to notify anyone.
* I disclaim any responsibility for any errors or lack of fitness for any purpose.
*
* cyclop Version 1.4.4 Oct. 13, 2022
*
* (Oct 13, 2022. Smarter ComputeLM. Eliminate mpz_pow() call from within for loops.)
* (May 24, 2018. Added capability for ComputeAllFactors() to add 1 and N as factors.)
* (Jun 30, 2016. Removed the pari dependency.)
* (Sep 22, 2014. Improved EulerTotient() -- skip computing all composite factors.)
* (Changes from version 1.03:
* -- Allow perfect powers for base.)
* (Changes from version 1.02:
* -- Some optimizations. Instead of trial factoring up to sqrt(n) for RemoveSquares(),
* ComputeAllFactors() etc., now only compute the prime factorization and then permute
* factors to compute the remaining composite factors.)
* (Changes from Version 1.01:
* -- minor tweak, change pari_init() to not initialize a large number of prime differences.
* this speeds up initialization quite a bit.)
* (Changes from Version 1.00:
* -- fix ComputePrimitive() not properly implemented.
* -- fix algorithm fail if exponent == 1
* -- don't allow exponents < 1 )
*
* To build try:
* g++ cyclop.cpp -lgmp -o cyclop
*
* (You will need to have g++ and the gmp library already installed)
* (For linux bash shell for Windows 10, try:
sudo apt-get install g++ libgmp3-dev )
*
* This source file is currently located at:
* http://myfactors.mooo.com/source/cyclop.cpp
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <values.h>
#include <gmp.h>
#include <string.h>
#include <ctype.h>
struct factor_info {
mpz_t the_factor;
long occurrences;
long digits;
char factor_status;
};
struct factor_infos {
long count;
struct factor_info* the_factors;
};
void ConvertPerfectPowers( mpz_t, mpz_t, mpz_t, mpz_t, mpz_t );
void ComputeLMPrimitives( mpz_t, mpz_t, mpz_t, char, mpz_t, mpz_t );
void ComputePrimitive( mpz_t, mpz_t, char, mpz_t );
void ComputeLongForm( mpz_t, mpz_t, char, mpz_t );
int IsLM( mpz_t, mpz_t, char );
int ComputeLM( mpz_t, mpz_t, mpz_t, char, mpz_t, mpz_t );
int ComputeLucasCD( mpz_t, mpz_t**, long*, mpz_t**, long* );
char quickprimecheck( mpz_t );
void TFDivideOut( mpz_t, long, char*, mpz_t, struct factor_infos* );
void Factorize( mpz_t, struct factor_infos* );
void PermuteFactors( struct factor_infos*, struct factor_infos*, long, mpz_t );
void ComputeAllFactors( mpz_t, struct factor_infos*, int = 0 );
long ComputeOccurrences( mpz_t, mpz_t );
int Mobius( mpz_t );
void EulerTotient( mpz_t, mpz_t );
void Init_Factor_Infos( struct factor_infos* );
void AddFactorInfo( struct factor_infos*, mpz_t, long, char, long );
void Cleanup_Factor_Infos( struct factor_infos* );
long NumberOfDigits( mpz_t );
void mpz_pow( mpz_t, mpz_t, mpz_t );
int factor_info_cmpfunc( const void*, const void* );
unsigned char wheel7[48] = { 2, 4, 2, 4, 6, 2, 6, 4, 2, 4,
6, 6, 2, 6, 4, 2, 6, 4, 6, 8,
4, 2, 4, 2, 4, 8, 6, 4, 6, 2,
4, 6, 2, 6, 6, 4, 2, 4, 6, 2,
6, 4, 2, 4, 2, 10, 2, 10 };
int main(int argc, char* argv[] )
{
if ( argc != 4 ) {
printf("\n");
printf("cyclop Version 1.4.4\n\n");
printf("Usage: \"cyclop base exponent +|-\"");
printf("\n\n");
return -1;
}
mpz_t base;
mpz_init( base );
mpz_set_str( base, argv[1], 10 );
if ( mpz_cmp_ui( base, 2 ) < 0 ) {
printf( "\n\nbase must be >= 2. Aborting.\n\n" );
mpz_clear( base );
return -1;
}
mpz_t exponent;
mpz_init( exponent );
mpz_set_str( exponent, argv[2], 10 );
if ( mpz_cmp_ui( exponent, 1 ) < 0 ) {
printf( "\n\nexponent cannot be less than 1. Aborting.\n\n" );
mpz_clear( exponent );
mpz_clear( base );
return -1;
}
char c0 = argv[3][0];
if ( c0 != '+' && c0 != '-' ) {
printf("\n\nThird argument must be either \'+\' or \'-\'.\n\n");
mpz_clear( exponent );
mpz_clear( base );
return -1;
}
mpz_t ConvertedBase;
mpz_init( ConvertedBase );
mpz_t ConvertedExponent;
mpz_init( ConvertedExponent );
mpz_t SqrFreeBase;
mpz_init( SqrFreeBase );
ConvertPerfectPowers( base, exponent, ConvertedBase, ConvertedExponent, SqrFreeBase );
if ( IsLM( SqrFreeBase, ConvertedExponent, c0 ) ) {
mpz_t LPrimitive;
mpz_init( LPrimitive );
mpz_t MPrimitive;
mpz_init( MPrimitive );
ComputeLMPrimitives( ConvertedBase, SqrFreeBase, ConvertedExponent, c0, LPrimitive, MPrimitive );
gmp_printf( "L-Primitive=%Zd\n", LPrimitive );
gmp_printf( "M-Primitive=%Zd\n", MPrimitive );
mpz_clear( MPrimitive );
mpz_clear( LPrimitive );
}
else {
mpz_t Primitive;
mpz_init( Primitive );
ComputePrimitive( ConvertedBase, ConvertedExponent, c0, Primitive );
gmp_printf( "Primitive=%Zd\n", Primitive );
mpz_clear( Primitive );
}
mpz_clear( ConvertedBase );
mpz_clear( ConvertedExponent );
mpz_clear( SqrFreeBase );
mpz_clear( exponent );
mpz_clear( base );
return 0;
}
// Need to fix base, exponent if base is a perfect power
// Compute the square-free part of base as well, since it's really easy to do.
void ConvertPerfectPowers( mpz_t base, mpz_t exponent, mpz_t ConvertedBase, mpz_t ConvertedExponent, mpz_t SqrFreeBase ) {
struct factor_infos PrimeFactorization;
Init_Factor_Infos( &PrimeFactorization );
Factorize( base, &PrimeFactorization );
mpz_t gcd;
mpz_init_set_ui( gcd, PrimeFactorization.the_factors[0].occurrences );
long i;
for ( i = 1; i < PrimeFactorization.count && mpz_cmp_ui( gcd, 1 ) != 0; i++ )
mpz_gcd_ui( gcd, gcd, PrimeFactorization.the_factors[i].occurrences );
long power = mpz_get_d( gcd );
mpz_set_ui( ConvertedBase, 1 );
mpz_mul_ui( ConvertedExponent, exponent, power );
mpz_t tempz1;
mpz_init( tempz1 );
mpz_set_ui( SqrFreeBase, 1 );
for ( i = 0; i < PrimeFactorization.count; i++ ) {
long reducedpower = PrimeFactorization.the_factors[i].occurrences / power;
mpz_pow_ui( tempz1, PrimeFactorization.the_factors[i].the_factor, reducedpower );
mpz_mul( ConvertedBase, ConvertedBase, tempz1 );
if ( reducedpower % 2 )
mpz_mul( SqrFreeBase, SqrFreeBase, PrimeFactorization.the_factors[i].the_factor );
}
mpz_clear( tempz1 );
mpz_clear( gcd );
Cleanup_Factor_Infos( &PrimeFactorization );
}
// Algorithm from "Factorizations of b^n ± 1, b = 2, 3, 5, 6, 7, 10, 11, 12 Up to High Powers", Third Edition, page lxxi,
// John Brillhart, D. H. Lehmer, J. L. Selfridge, Bryant Tuckerman, and S. S. Wagstaff, Jr.
// Correction information for bases with odd square factors provided by user "jyb" on:
// http://www.mersenneforum.org/showpost.php?p=186049&postcount=5
// Assumption: base is not a perfect power
void ComputeLMPrimitives( mpz_t base, mpz_t sqrfreebase, mpz_t exponent, char c0, mpz_t LPrimitive, mpz_t MPrimitive ) {
mpz_t m; // odd part of exponent
mpz_init_set( m, exponent );
while ( mpz_even_p( m ) )
mpz_div_2exp( m, m, 1 );
struct factor_infos Factor_Infos;
Init_Factor_Infos( &Factor_Infos );
ComputeAllFactors( m, &Factor_Infos, 1 );
mpz_t LPrimNumerator;
mpz_init_set_ui( LPrimNumerator, 1 );
mpz_t LPrimDenominator;
mpz_init_set_ui( LPrimDenominator, 1 );
mpz_t MPrimNumerator;
mpz_init_set_ui( MPrimNumerator, 1 );
mpz_t MPrimDenominator;
mpz_init_set_ui( MPrimDenominator, 1 );
mpz_t gcd;
mpz_init( gcd );
mpz_t d;
mpz_init( d );
long i = 0;
for ( ; i < Factor_Infos.count; i++ ) {
mpz_set( d, Factor_Infos.the_factors[i].the_factor );
mpz_gcd( gcd, sqrfreebase, d );
if ( mpz_cmp_ui( gcd, 1 ) != 0 )
continue;
mpz_t L;
mpz_init( L );
mpz_t M;
mpz_init( M );
mpz_t cofactor;
mpz_init( cofactor );
mpz_divexact( cofactor, exponent, d );
ComputeLM( base, sqrfreebase, cofactor, c0, L, M );
int jacobi = mpz_jacobi( sqrfreebase, d );
int mobius = Mobius( d );
if ( mobius != 0 ) {
mpz_mul( mobius == 1 ? LPrimNumerator : LPrimDenominator, mobius == 1 ? LPrimNumerator : LPrimDenominator, jacobi == 1 ? L : M );
mpz_mul( mobius == 1 ? MPrimNumerator : MPrimDenominator, mobius == 1 ? MPrimNumerator : MPrimDenominator, jacobi == 1 ? M : L );
}
mpz_clear( cofactor );
mpz_clear( M );
mpz_clear( L );
}
mpz_divexact( LPrimitive, LPrimNumerator, LPrimDenominator );
mpz_divexact( MPrimitive, MPrimNumerator, MPrimDenominator );
mpz_clear( d );
mpz_clear( gcd );
mpz_clear( MPrimDenominator );
mpz_clear( MPrimNumerator );
mpz_clear( LPrimDenominator );
mpz_clear( LPrimNumerator );
Cleanup_Factor_Infos( &Factor_Infos );
mpz_clear( m );
}
// Algorithm from A. Kruppa's phi.c found at <http://www.asahi-net.or.jp/~KC2H-MSM/mathland/matha1/matha152x.htm>
void ComputePrimitive( mpz_t base, mpz_t exponent, char c0, mpz_t Primitive ) {
if ( mpz_cmp_ui( exponent, 1 ) == 0 ) {
ComputeLongForm( base, exponent, c0, Primitive );
return;
}
mpz_t phi_exponent;
mpz_init_set( phi_exponent, exponent );
if ( c0 == '+' )
mpz_mul_2exp( phi_exponent, phi_exponent, 1 ); // multiply by 2^1
mpz_t Rp;
mpz_init_set_ui( Rp, 1 );
mpz_t Rq;
mpz_init_set_ui( Rq, 1 );
mpz_t tempz1;
mpz_init( tempz1 );
struct factor_infos Factor_Infos;
Init_Factor_Infos( &Factor_Infos );
ComputeAllFactors( phi_exponent, &Factor_Infos, 1 );
long i = 0;
for ( ; i < Factor_Infos.count; i++ ) {
ComputeLongForm( base, Factor_Infos.the_factors[i].the_factor, '-', tempz1 );
mpz_t cofactor;
mpz_init( cofactor );
mpz_divexact( cofactor, phi_exponent, Factor_Infos.the_factors[i].the_factor );
switch ( Mobius( cofactor ) ) {
case 1:
mpz_mul( Rp, Rp, tempz1 );
break;
case -1:
mpz_mul( Rq, Rq, tempz1 );
break;
case 0:
default:
break;
}
mpz_clear( cofactor );
}
mpz_divexact( Primitive, Rp, Rq );
Cleanup_Factor_Infos( &Factor_Infos );
mpz_clear( tempz1 );
mpz_clear( Rq );
mpz_clear( Rp );
mpz_clear( phi_exponent );
}
// Compute the value base^exponent + c0
void ComputeLongForm( mpz_t base, mpz_t exponent, char c0, mpz_t the_number ) {
if ( mpz_cmp_ui( base, 2 ) == 0 ) {
mpz_t number1;
mpz_init_set_ui( number1, 1 );
mp_bitcnt_t bitcount = mpz_get_ui( exponent );
mpz_mul_2exp( the_number, number1, bitcount );
if ( c0 == '-' )
mpz_sub( the_number, the_number, number1 );
else
mpz_add( the_number, the_number, number1 );
mpz_clear( number1 );
return;
}
if ( mpz_cmp_ui( exponent, ULONG_MAX ) > 0 ) {
printf("Error: exponent must be <= %lu\n", ULONG_MAX );
}
else {
mpz_t number1;
mpz_init_set_ui( number1, 1 );
unsigned long ulexp = mpz_get_ui( exponent );
mpz_pow_ui( the_number, base, ulexp );
if ( c0 == '-' )
mpz_sub( the_number, the_number, number1 );
else
mpz_add( the_number, the_number, number1 );
mpz_clear( number1 );
}
}
// Returns 1 if base^exponent + c0 has an Aurifeuillian factorization else 0.
// Assumption: base is not a perfect power and is square-free
int IsLM( mpz_t base, mpz_t exponent, char c0 ) {
// Not handled values
if ( mpz_cmp_ui( base, 2 ) < 0 ||
mpz_cmp_ui( exponent, 2 ) < 0 ||
(c0 != '-' && c0 != '+') ) {
return 0;
}
{ // check that we have the correct c0. c0 should be -1 if base == 1 mod 4, else c0 should be 1
mpz_t r;
mpz_init( r );
mpz_mod_ui( r, base, 4 );
if ( ( (c0 == '+') && mpz_cmp_ui( r, 1 ) == 0) ||
( (c0 == '-') && mpz_cmp_ui( r, 1 ) != 0) ) {
mpz_clear( r );
return 0;
}
mpz_clear( r );
}
if ( !mpz_divisible_p( exponent, base ) ) // not divisible. L,M do not exist
return 0;
mpz_t h;
mpz_init( h );
mpz_divexact( h, exponent, base );
if ( mpz_even_p( h ) ) { // h is even. L,M do not exist
mpz_clear( h );
return 0;
}
mpz_clear( h );
return 1;
}
// Compute C and D polynomial coefficients. Code implemented based on
// Richard Brent's "On computing factors of cyclotomic polynomials" Algorithm L.
int ComputeLucasCD( mpz_t n, mpz_t** C, long* C_count, mpz_t** D, long* D_count ) {
mpz_t tempz1;
mpz_init( tempz1 );
mpz_t tempz2;
mpz_init( tempz2 );
mpz_t nmod4;
mpz_init( nmod4 );
mpz_mod_ui( nmod4, n, 4 );
mpz_t nmod8;
mpz_init( nmod8 );
mpz_mod_ui( nmod8, n, 8 );
mpz_t nprime;
mpz_init( nprime );
if ( mpz_cmp_ui( nmod4, 1 ) == 0 )
mpz_set( nprime, n );
else
mpz_mul_ui( nprime, n, 2 );
mpz_t s;
mpz_init( s );
if ( mpz_cmp_ui( nmod4, 3 ) == 0 )
mpz_set_si( s, -1 );
else
mpz_set_ui( s, 1 );
mpz_t sprime;
mpz_init( sprime );
if ( mpz_cmp_ui( nmod8, 5 ) == 0 )
mpz_set_si( sprime, -1 );
else
mpz_set_ui( sprime, 1 );
mpz_t d;
mpz_init( d );
EulerTotient(nprime, tempz1 );
mpz_divexact_ui( d, tempz1, 2 );
// Step 1
long ld = mpz_get_ui( d );
mpz_t* q = (mpz_t*) calloc( ld + 1, sizeof( mpz_t ) );
long k;
for ( k = 0; k <= ld; k++ )
mpz_init_set_ui( q[k], 0 );
mpz_t mpz_k;
mpz_init( mpz_k );
for ( k = 1; k <= ld; k++ ) {
mpz_set_ui( mpz_k, k );
if ( k % 2 )
mpz_set_si( q[k], mpz_jacobi( n, mpz_k ) );
else {
mpz_t gprime_k;
mpz_init( gprime_k );
mpz_gcd( gprime_k, mpz_k, nprime );
mpz_t tempz1;
mpz_init( tempz1 );
mpz_divexact( tempz1, nprime, gprime_k );
int mobius_value = Mobius( tempz1 );
mpz_t mpz_totient_value;
mpz_init( mpz_totient_value );
EulerTotient( gprime_k, mpz_totient_value );
int cosine_value = 0;
{
mpz_sub_ui( tempz1, n, 1 );
mpz_mul( tempz1, tempz1, mpz_k );
mpz_t mod8;
mpz_init( mod8 );
mpz_mod_ui( mod8, tempz1, 8 );
// we won't check for odd values since k is even and therefore mod8 is also even
if ( mpz_cmp_ui( mod8, 0 ) == 0 )
cosine_value = 1;
else if ( mpz_cmp_ui( mod8, 2 ) == 0 )
cosine_value = 0;
else if ( mpz_cmp_ui( mod8, 4 ) == 0 )
cosine_value = -1;
else // mod8 == 6
cosine_value = 0;
mpz_clear( mod8 );
}
mpz_mul_si( tempz1, mpz_totient_value, mobius_value );
mpz_mul_si( tempz1, tempz1, cosine_value );
mpz_set( q[k], tempz1 );
mpz_clear( mpz_totient_value );
mpz_clear( tempz1 );
mpz_clear( gprime_k );
}
}
mpz_t* gamma = (mpz_t*) calloc( ld + 1, sizeof( mpz_t ) );
for ( k = 0; k <= ld; k++ )
mpz_init_set_ui( gamma[k], 0 );
mpz_t* delta = (mpz_t*) calloc( ld, sizeof( mpz_t ) );
for ( k = 0; k < ld; k++ )
mpz_init_set_ui( delta[k], 0 );
// Step 2
mpz_set_ui( gamma[0], 1 );
mpz_set_ui( delta[0], 1 );
// Step 3
int d_is_odd = ld % 2;
long max_gamma = d_is_odd ? ( ld - 1 ) / 2 : ld / 2;
long max_delta = d_is_odd ? ( ld - 1 ) / 2 : ( ld - 2 ) / 2;
for ( k = 1; k <= max_gamma; k++ ) {
// compute gamma[k]
long j = 0;
for ( j = 0; j <= k - 1; j++ ) {
long q_index = 2 * k - 2 * j - 1;
mpz_mul( tempz1, n, q[q_index] );
mpz_mul( tempz1, tempz1, delta[j] );
mpz_add( gamma[k], gamma[k], tempz1 );
q_index++;
mpz_mul( tempz1, q[q_index], gamma[j] );
mpz_sub( gamma[k], gamma[k], tempz1 );
}
mpz_divexact_ui( gamma[k], gamma[k], 2 * k );
if ( k > max_delta )
continue;
// compute delta[k]
for ( j = 0; j <= k - 1; j++ ) {
long q_index = 2 * k + 1 - 2 * j;
mpz_mul( tempz1, q[q_index], gamma[j] );
mpz_add( delta[k], delta[k], tempz1 );
q_index--;
mpz_mul( tempz1, q[q_index], delta[j] );
mpz_sub( delta[k], delta[k], tempz1 );
}
mpz_add( delta[k], delta[k], gamma[k] );
mpz_divexact_ui( delta[k], delta[k], 2 * k + 1 );
}
// Step 4
for ( k = max_gamma + 1; k <= ld; k++ )
mpz_set( gamma[k], gamma[ld-k] );
10000
// Step 5
for ( k = max_delta + 1; k < ld; k++ )
mpz_set( delta[k], delta[ld-1-k] );
// Pass back
*C = gamma;
*C_count = ld + 1;
*D = delta;
*D_count = ld;
// clean up
mpz_clear( mpz_k );
if ( q != NULL ) {
for ( k = 0; k <= ld; k++ )
mpz_clear( q[k] );
free( q );
q = NULL;
}
mpz_clear( d );
mpz_clear( sprime );
mpz_clear( s );
mpz_clear( nprime );
mpz_clear( nmod8 );
mpz_clear( nmod4 );
mpz_clear( tempz2 );
mpz_clear( tempz1 );
return 0;
}
// Computes the Aurifeuillian factors L,M for a^x +/- 1. Returns 0 on success.
// Assumption: base is not a perfect power
int ComputeLM( mpz_t base, mpz_t sqrfreebase, mpz_t exponent, char c0, mpz_t L, mpz_t M ) {
// Not handled values
if ( mpz_cmp_ui( base, 2 ) < 0 ||
mpz_cmp_ui( exponent, 2 ) < 0 ||
(c0 != '-' && c0 != '+') ) {
return -1;
}
{ // check that we have the correct c0. c0 should be -1 if sqrfreebase == 1 mod 4, else c0 should be 1
mpz_t r;
mpz_init( r );
mpz_mod_ui( r, sqrfreebase, 4 );
if ( (c0 == '+' && mpz_cmp_ui( r, 1 ) == 0) ||
(c0 == '-' && mpz_cmp_ui( r, 1 ) != 0) ) {
mpz_clear( r );
return -1;
}
mpz_clear( r );
}
if ( !mpz_divisible_p( exponent, sqrfreebase ) ) // not divisible. L,M do not exist
return -1;
unsigned long h = 0;
{
mpz_t temph;
mpz_init( temph );
mpz_divexact( temph, exponent, sqrfreebase );
h = mpz_get_ui( temph );
mpz_clear( temph );
if ( (h % 2) == 0 ) // h is even. L,M do not exist
return -1;
}
mpz_t* C = NULL;
long C_count = 0;
mpz_t* D = NULL;
long D_count = 0;
if ( ComputeLucasCD( sqrfreebase, &C, &C_count, &D, &D_count ) != 0 ) {
gmp_printf("Error: Could not compute Lucas C,D polynomial coefficients for base (%Zd). Aborting.\n", sqrfreebase );
return -1;
}
mpz_t tempz1;
mpz_init( tempz1 );
unsigned long k = (h + 1) / 2;
// Both Polynomial A & B are initially computed as follows:
//
// coef[0] * base^(h*0) + coef[1] * base^(h*1) + coef[2] * base^(h*2) + ...
// = coef[0] * (base^h)^0 + coef[1] * (base^h)^1 + coef[2] * (base^h)^2 + ...
// = coef[0] * 1 + coef[1] * base^h + coef[2] * base^h * base^h + ...
//
// B polynomial is then multiplied by additional terms
//
// Then,
// L = A - B
// M = A + B
// NOTE: Reading coefficients from beginning to end is exactly the same as reading from the end to the beginning.
// eg. for base 31, C = { 1,15,43,83,125,151,169,173,173,169,151,125,83,43,15,1 } and D = { 1,5,11,19,25,29,31,31,31,29,25,19,11,5,1 }
// Also, D_count = C_count - 1
// Compute Polynomial A with the Lucas 'C' coefficients
mpz_t A;
mpz_init( A );
long i;
{
mpz_t base_h; // base^h
mpz_init( base_h );
mpz_pow_ui( base_h, base, h );
mpz_t base_raised; // base_h^i
mpz_init( base_raised );
for ( i = 0; i < C_count; i++ ) { // each iteration computes one term and then adds to A
if ( i == 0 )
mpz_set_ui( base_raised, 1 );
else if ( i == 1 )
mpz_set( base_raised, base_h );
else
mpz_mul( base_raised, base_raised, base_h );
mpz_mul( tempz1, base_raised, C[i] );
mpz_add( A, A, tempz1 );
}
mpz_clear( base_raised );
mpz_clear( base_h );
}
// Compute Polynomial B with Lucas 'D' coefficients
mpz_t B;
mpz_init( B );
{
mpz_t base_h; // base^h
mpz_init( base_h );
mpz_pow_ui( base_h, base, h );
mpz_t base_raised; // base_h^i
mpz_init( base_raised );
for ( i = 0; i < D_count; i++ ) { // each iteration computes one term and then adds to B
if ( i == 0 )
mpz_set_ui( base_raised, 1 );
else if ( i == 1 )
mpz_set( base_raised, base_h );
else
mpz_mul( base_raised, base_raised, base_h );
mpz_mul( tempz1, base_raised, D[i] );
mpz_add( B, B, tempz1 );
}
mpz_clear( base_raised );
mpz_clear( base_h );
}
if ( mpz_cmp( base, sqrfreebase ) != 0 ) {
mpz_divexact( tempz1, base, sqrfreebase );
mpz_sqrt( tempz1, tempz1 );
mpz_pow_ui( tempz1, tempz1, h );
mpz_mul( B, B, tempz1 );
}
mpz_pow_ui( tempz1, sqrfreebase, k );
mpz_mul( B, B, tempz1 );
mpz_sub( L, A, B );
mpz_add( M, A, B );
mpz_clear( B );
mpz_clear( A );
mpz_clear( tempz1 );
for ( i = 0; i < C_count; i++ )
mpz_clear( C[i] );
free( C );
C = NULL;
for ( i = 0; i < D_count; i++ )
mpz_clear( D[i] );
free( D );
D = NULL;
return 0;
}
char quickprimecheck( mpz_t the_number ) {
char retval = 'C';
if ( mpz_cmp_ui( the_number, 1 ) == 0 ) {
retval = 'N';
return retval;
}
int factor_type = mpz_probab_prime_p( the_number, 30 );
if ( factor_type != 0 )
retval = 'P';
return retval;
}
// divide out denominator from running_N
void TFDivideOut( mpz_t running_N, long ldenominator, char* running_N_status, mpz_t square_root, struct factor_infos* Factor_Infos ) {
mpz_t denominator;
mpz_init_set_ui( denominator, ldenominator );
long occurrences = ComputeOccurrences( running_N, denominator );
long i = 0;
for ( i = occurrences; i > 0; i-- )
mpz_divexact_ui( running_N, running_N, ldenominator );
*running_N_status = quickprimecheck( running_N );
if ( *running_N_status == 'N' )
mpz_set_ui( square_root, 1 );
else
mpz_sqrt( square_root, running_N );
AddFactorInfo( Factor_Infos, denominator, occurrences, 'P', NumberOfDigits( denominator ) );
mpz_clear( denominator );
}
// Compute the prime factorization of a general number where square_root( the_number ) < MAXLONG - 255
void Factorize( mpz_t the_number, struct factor_infos* Factor_Infos ) {
if ( Factor_Infos == NULL )
return;
// Wheel Factorization. eg. http://programmingpraxis.com/2009/05/08/wheel-factorization/
mpz_t running_N;
mpz_init_set( running_N, the_number );
char running_N_status = quickprimecheck( running_N );
if ( running_N_status == 'P' ) {
AddFactorInfo( Factor_Infos, running_N, 1, 'P', NumberOfDigits( running_N ) );
mpz_clear( running_N );
return;
}
mpz_t square_root;
mpz_init( square_root );
mpz_sqrt( square_root, running_N );
if ( mpz_cmp_ui( square_root, MAXLONG - 255 ) > 0 ) {
printf( "Internal Error: Trying to factorize too big a number.\n" );
mpz_clear( square_root );
mpz_clear( running_N );
return;
}
if ( mpz_divisible_ui_p( running_N, 2 ) != 0 ) {
TFDivideOut( running_N, 2, &running_N_status, square_root, Factor_Infos );
if ( running_N_status == 'P' ) {
AddFactorInfo( Factor_Infos, running_N, 1, 'P', NumberOfDigits( running_N ) );
mpz_clear( square_root );
mpz_clear( running_N );
return;
}
}
if ( mpz_divisible_ui_p( running_N, 3 ) != 0 ) {
TFDivideOut( running_N, 3, &running_N_status, square_root, Factor_Infos );
if ( running_N_status == 'P' ) {
AddFactorInfo( Factor_Infos, running_N, 1, 'P', NumberOfDigits( running_N ) );
mpz_clear( square_root );
mpz_clear( running_N );
return;
}
}
if ( mpz_divisible_ui_p( running_N, 5 ) != 0 ) {
TFDivideOut( running_N, 5, &running_N_status, square_root, Factor_Infos );
if ( running_N_status == 'P' ) {
AddFactorInfo( Factor_Infos, running_N, 1, 'P', NumberOfDigits( running_N ) );
mpz_clear( square_root );
mpz_clear( running_N );
return;
}
}
if ( mpz_divisible_ui_p( running_N, 7 ) != 0 ) {
TFDivideOut( running_N, 7, &running_N_status, square_root, Factor_Infos );
if ( running_N_status == 'P' ) {
AddFactorInfo( Factor_Infos, running_N, 1, 'P', NumberOfDigits( running_N ) );
mpz_clear( square_root );
mpz_clear( running_N );
return;
}
}
unsigned long i = 0;
unsigned long denominator = 11;
for ( ;
running_N_status == 'C';
denominator += wheel7[i], i = ( i == 47 ? 0 : i + 1 ) ) {
if ( mpz_divisible_ui_p( running_N, denominator ) != 0 )
TFDivideOut( running_N, denominator, &running_N_status, square_root, Factor_Infos );
}
if ( running_N_status == 'P' )
AddFactorInfo( Factor_Infos, running_N, 1, 'P', NumberOfDigits( running_N ) );
mpz_clear( square_root );
mpz_clear( running_N );
}
void PermuteFactors( struct factor_infos* permutefactors, struct factor_infos* allfactors, long lefttumbler, mpz_t leftproduct, mpz_t the_number ) {
long tumbler = lefttumbler + 1;
mpz_t currfactor;
mpz_init( currfactor );
mpz_t currproduct;
mpz_init( currproduct );
long exponent;
for ( exponent = 0; exponent <= permutefactors->the_factors[tumbler].occurrences; exponent++ ) {
// compute primefactor^exponent
if ( exponent == 0 )
mpz_set_ui( currfactor, 1 );
else if ( exponent == 1 )
mpz_set( currfactor, permutefactors->the_factors[tumbler].the_factor );
else
mpz_mul( currfactor, currfactor, permutefactors->the_factors[tumbler].the_factor );
mpz_mul( currproduct, leftproduct, currfactor );
if ( tumbler < permutefactors->count - 1 )
PermuteFactors( permutefactors, allfactors, tumbler, currproduct, the_number );
else if ( mpz_cmp_ui( currproduct, 1 ) != 0 && mpz_cmp( currproduct, the_number ) != 0 ) {
char status = quickprimecheck( currproduct );
long occurrences = ComputeOccurrences( the_number, currproduct );
AddFactorInfo( allfactors, currproduct, occurrences, status, NumberOfDigits( currproduct ) );
}
}
mpz_clear( currproduct );
mpz_clear( currfactor );
}
// Compute all factors of the_number (Trivial factors of 1 and N defaulting to not be included)
void ComputeAllFactors( mpz_t N, struct factor_infos* Factor_Infos, int AddTrivialFactors ) {
if ( Factor_Infos == NULL )
return;
if ( quickprimecheck( N ) == 'C' ) {
struct factor_infos primes_only;
Init_Factor_Infos( &primes_only );
Factorize( N, &primes_only );
mpz_t leftproduct;
mpz_init_set_ui( leftproduct, 1 );
PermuteFactors( &primes_only, Factor_Infos, -1, leftproduct, N );
mpz_clear( leftproduct );
Cleanup_Factor_Infos( &primes_only );
}
if ( AddTrivialFactors ) {
mpz_t tempZ;
mpz_init_set_ui( tempZ, 1 );
AddFactorInfo( Factor_Infos, tempZ, 1, 'N', 1 );
if ( mpz_cmp_ui( N, 1 ) != 0 )
AddFactorInfo( Factor_Infos, N, 1, '?', NumberOfDigits( N ) );
mpz_clear( tempZ );
}
qsort( Factor_Infos->the_factors, Factor_Infos->count, sizeof(struct factor_info), factor_info_cmpfunc );
}
// Compute the number of times denominator divides evenly into numerator
long ComputeOccurrences( mpz_t numerator, mpz_t denominator ) {
// not handled values
if ( mpz_cmp_ui( denominator, 1 ) <= 0 )
return -1;
long occurrences = 0;
mpz_t quotient;
mpz_init_set( quotient, numerator );
while ( mpz_divisible_p( quotient, denominator ) ) {
mpz_divexact( quotient, quotient, denominator );
occurrences++;
}
mpz_clear( quotient );
return occurrences;
}
// Compute the Mobius function
int Mobius( mpz_t n ) {
if ( mpz_cmp_ui( n, 1 ) == 0 )
return 1;
long total_occurrences = 0;
int has_duplicate_occurrences = 0;
if ( quickprimecheck( n ) == 'P' ) {
total_occurrences++;
}
else {
struct factor_infos Factor_Infos;
Init_Factor_Infos( &Factor_Infos );