-
Notifications
You must be signed in to change notification settings - Fork 8
/
eval.c
988 lines (918 loc) · 25.2 KB
/
eval.c
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
/* Simple expression parser for GMP-ECM.
Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2012 Jim Fougeron, Paul Zimmermann and Alexander Kruppa.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, see
http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "ecm-ecm.h"
#include "getprime_r.h"
#ifdef HAVE_STRINGS_H
# include <strings.h> /* for strncasecmp */
#endif
#ifdef HAVE_CTYPE_H
# include <ctype.h>
#endif
/*****************************************************************
* Syntax for this VERY simple recursive expression parser: *
* *
* ( or [ or { along with ) or ] or } are valid for grouping *
* Normal "simple" operators: + - * / (. can be used for *) *
* Module: n%m 345%11 *
* Unary minus is supported: -n -500 *
* Exponentation: n^m 2^500 *
* Simple factorial: n! 53! == 1*2*3*4...*52*53 *
* Multi-factorial: n!m 15!3 == 15.12.9.6.3 *
* Simple Primorial: n# 11# == 2*3*5*7*11 *
* Reduced Primorial: n#m 17#5 == 5.7.11.13.17 *
* *
* Supported functions: (case insensitive) *
* Phi(n,x) Phi(3,5) == 1+x+x^2 = 31 *
* GCD(m,n) GCD(120, 28) = 4 *
* U(p,q,n) *
* primU(p,q,n) *
* TODO: PhiL(k,n), PhiM(k,n) *
* only for bases 2,3,5,6,7,10,11 (times a square) *
* Note for developers: *
* First k-1 arguments are passed as an mpz_t array *
* *
* NOTE Lines ending in a \ character are "joined" *
* NOTE C++ // single line comments (rest of line is a comment) *
* *
****************************************************************/
/* value only used by the expression parser */
static mpz_t t, mpOne;
static char *expr_str;
static void eval_power (mpz_t prior_n, mpz_t n,char op);
static void eval_product (mpz_t prior_n, mpz_t n,char op);
static void eval_sum (mpz_t prior_n, mpz_t n,char op);
static int eval_Phi (mpz_t *params, mpz_t n);
static int eval_PhiL (mpz_t *params, mpz_t n);
static int eval_PhiM (mpz_t *params, mpz_t n);
// static int eval_gcd (mpz_t *params, mpz_t n);
static int eval_U (mpz_t *params, mpz_t n);
static int eval_primU (mpz_t *params, mpz_t n);
static int eval_2 (int bInFuncParams);
static int aurif (mpz_t output, mpz_t n, mpz_t base, int sign);
#if 0 /* strncasecmp is a required function in configure.in */
#if defined (_MSC_VER) || defined (__MINGW32__)
#define strncasecmp strnicmp
#endif
#endif
/***************************************/
/* Main expression evaluation function */
/* This is the function that the app */
/* calls to read the expression line */
/***************************************/
int eval (mpcandi_t *n, FILE *fd, int primetest)
{
int ret;
int nMaxSize = 2000, nCurSize = 0;
int c;
char *expr = (char *) malloc (nMaxSize + 1);
ASSERT_ALWAYS (expr != NULL);
JoinLinesLoop:
c = fgetc (fd);
if (0)
{
ChompLine:
do
c = fgetc (fd);
while (c != EOF && !IS_NEWLINE(c));
if (IS_NEWLINE(c))
goto JoinLinesLoop;
}
while (c != EOF && !IS_NEWLINE(c) && c != ';')
{
if (c == '/')
{
/* This might be a C++ // comment or it might be a / division operator.
Check it out, and if it is a comment, then "eat it" */
int peek_c = fgetc (fd);
if (peek_c == '/')
/* Got a C++ single line comment, so Chomp the line */
goto ChompLine;
/* Put the char back on the file, then allow the code to add the '/' char to the buffer */
ungetc (peek_c, fd);
}
/* strip space and tabs out here, and then we DON'T have to mess with them in the rest of the parser */
if (!isspace (c) && c != '"' && c != '\'')
expr[nCurSize++] = (char) c;
if (nCurSize == nMaxSize)
{
char *cp;
nMaxSize += nMaxSize / 2;
cp = (char *) realloc (expr, nMaxSize + 1);
ASSERT_ALWAYS (cp != NULL);
expr = cp;
}
c = fgetc (fd);
}
expr[nCurSize] = 0;
if (!nCurSize)
ret = 0;
else
{
if (c == ';')
ungetc (c, fd);
mpz_init (t);
expr_str = expr;
ret = eval_2 (0);
if (ret)
{
char *s;
char *cpTmpExpr = expr;
s = mpz_get_str (NULL, 10, t);
if (!strcmp(s, cpTmpExpr))
cpTmpExpr = NULL;
ret = mpcandi_t_add_candidate (n, t, cpTmpExpr, primetest);
free (s); /* size strlen (s) + 1 */
}
mpz_clear(t);
}
free(expr);
return ret;
}
int eval_str (mpcandi_t *n, char *cp, int primetest, char **EndChar)
{
int ret;
int nMaxSize=2000, nCurSize=0;
char *c;
char *expr = (char *) malloc(nMaxSize+1);
ASSERT_ALWAYS (expr != NULL);
c = cp;
JoinLinesLoop:
if (*c == '#')
{
do
++c;
while (*c && !IS_NEWLINE(*c));
if (IS_NEWLINE(*c))
goto JoinLinesLoop;
}
while (*c && !IS_NEWLINE(*c) && *c != ';')
{
/* strip space and tabs out here, and then we DON'T have to mess with them in the rest of the parser */
if (!isspace((int) *c) && *c != '"' && *c != '\'')
expr[nCurSize++] = *c;
if (nCurSize == nMaxSize)
{
char *cp;
nMaxSize += nMaxSize / 2;
cp = (char *) realloc (expr, nMaxSize + 1);
ASSERT_ALWAYS (cp != NULL);
expr = cp;
}
++c;
}
expr[nCurSize] = 0;
if (!nCurSize)
ret = 0;
else
{
if (*c != ';')
++c;
mpz_init(t);
expr_str = expr;
ret = eval_2(0);
if (ret)
{
char *s;
char *cpTmpExpr = expr;
s = mpz_get_str (NULL, 10, t);
if (!strcmp(s, cpTmpExpr))
cpTmpExpr = NULL;
ret = mpcandi_t_add_candidate(n, t, cpTmpExpr, primetest);
free (s); /* size strlen (s) + 1 */
}
mpz_clear(t);
}
free(expr);
if (EndChar && *EndChar)
*EndChar = c;
return ret;
}
void eval_power (mpz_t prior_n, mpz_t n,char op)
{
#if defined (DEBUG_EVALUATOR)
if ('#'==op || '^'==op || '!'==op || '@'==op || '$'==op)
{
gmp_fprintf (stderr, "eval power %Zd%c%Zd\n", prior_n, op, n);
}
#endif
if ('^'==op)
mpz_pow_ui(n,prior_n,mpz_get_ui(n));
else if ('!'==op) /* simple factorial (syntax n! example: 7! == 1*2*3*4*5*6*7) */
mpz_fac_ui(n,mpz_get_ui(n));
else if ('@'==op) /* Multi factorial (syntax n!prior_n
Example: 15!3 == 15*12*9*6*3
Note: 15!3 is substituted into 15@3 by the parser */
{
long nCur;
unsigned long nDecr;
nCur = mpz_get_si(prior_n);
nDecr = mpz_get_ui(n);
mpz_set_ui(n,1);
while (nCur > 1)
{
/* This could be done much more efficiently (bunching mults using smaller "built-ins"), but I am not going to bother for now */
mpz_mul_ui(n,n,nCur);
nCur -= nDecr;
}
}
else if ('#'==op) /* simple primorial (syntax n# example: 11# == 2*3*5*7*11 */
{
unsigned long nMax;
unsigned long p;
prime_info_t prime_info;
prime_info_init (prime_info);
nMax = mpz_get_ui (n);
mpz_set_ui (n, 1);
for (p = 2; p <= nMax; p = getprime_mt (prime_info))
/* This could be done much more efficiently (bunching mults using smaller "built-ins"), but I am not going to bother for now */
mpz_mul_ui (n, n, p);
prime_info_clear (prime_info); /* free the prime table */
}
else if ('$'==op) /* reduced primorial (syntax n#prior_n example: 13#5 == (5*7*11*13) */
{
unsigned long p;
unsigned long nMax;
unsigned long nStart;
prime_info_t prime_info;
prime_info_init (prime_info);
nMax = mpz_get_ui (prior_n);
nStart = mpz_get_ui (n);
mpz_set_ui (n, 1);
/*printf ("Reduced-primorial %ld#%ld\n", nMax, nStart);*/
for (p = 2; p <= nMax; p = getprime_mt (prime_info))
{
if (p >= nStart)
/* This could be done much more efficiently (bunching mults using smaller "built-ins"), but I am not going to bother for now */
mpz_mul_ui (n, n, p);
}
prime_info_clear (prime_info); /* free the prime table */
}
}
void
eval_product (mpz_t prior_n, mpz_t n, char op)
{
#if defined (DEBUG_EVALUATOR)
if ('*'==op || '.'==op || '/'==op || '%'==op)
{
gmp_fprintf (stderr, "eval_product %Zd%c%Zd\n", prior_n, op, n);
}
#endif
if ('*' == op || '.' == op)
mpz_mul (n, prior_n, n);
else if ('/' == op)
{
mpz_t r;
mpz_init (r);
mpz_tdiv_qr (n, r, prior_n, n);
if (mpz_cmp_ui (r, 0) != 0)
{
fprintf (stderr, "Parsing Error: inexact division\n");
exit (EXIT_FAILURE);
}
mpz_clear (r);
}
else if ('%' == op)
mpz_tdiv_r (n, prior_n, n);
}
void eval_sum (mpz_t prior_n, mpz_t n,char op)
{
#if defined (DEBUG_EVALUATOR)
if ('+'==op || '-'==op)
{
gmp_fprintf (stderr, "eval_sum %Zd%c%Zd\n", prior_n, op, n);
}
#endif
if ('+' == op)
mpz_add(n,prior_n,n);
else if ('-' == op)
mpz_sub(n,prior_n,n);
}
int eval_Phi (mpz_t* params, mpz_t n)
{
/* params[0]=exp, n=base */
int factors[200];
unsigned dwFactors=0, dw;
unsigned long B;
unsigned long p;
mpz_t D, T, org_n;
prime_info_t prime_info;
/* deal with trivial cases first */
if (mpz_cmp_ui (params[0], 0) == 0)
{
mpz_set_ui (n, 1);
return 1;
}
if (mpz_cmp_ui (params[0], 0) < 0)
return 0;
if (mpz_cmp_ui (params[0], 1) == 0)
{
mpz_sub_ui (n, n, 1);
return 1;
}
if (mpz_cmp_ui (params[0], 2) == 0)
{
mpz_add_ui (n, n, 1);
return 1;
}
if (mpz_cmp_ui (n, 0) < 0)
/* Convert to positive base; this is always valid when exp>=3 */
{
mpz_neg (n, n);
if (mpz_congruent_ui_p (params[0], 1, 2))
{
mpz_mul_ui(params[0], params[0], 2);
}
else if (mpz_congruent_ui_p (params[0], 2, 4))
{
mpz_divexact_ui(params[0], params[0], 2);
}
}
if (mpz_cmp_ui (n, 1) == 0)
{
/* return value is p if params[0] is prime power p^k, or 1 otherwise */
int maxpower=mpz_sizeinbase(params[0], 2)+1;
mpz_init (T);
for (int power=maxpower; power>=1; --power)
{
if ( mpz_root (T, params[0], power) ) break;
}
int isPrime = mpz_probab_prime_p (T, PROBAB_PRIME_TESTS);
mpz_set (n, isPrime ? T : mpOne);
mpz_clear(T);
return 1;
}
/* Ok, do the real h_primative work, since we are not one of the trivial case */
if (mpz_fits_ulong_p (params[0]) == 0)
return 0;
B = mpz_get_ui (params[0]);
/* Obtain the factors of B */
prime_info_init (prime_info);
for (p = 2; p <= B; p = getprime_mt (prime_info))
{
if (B % p == 0)
{
/* Add the factor one time */
factors[dwFactors++] = p;
/* but be sure to totally remove it */
do { B /= p; } while (B % p == 0);
}
}
prime_info_clear (prime_info); /* free the prime tables */
B = mpz_get_si (params[0]);
mpz_init_set (org_n, n);
mpz_set_ui (n, 1);
mpz_init_set_ui (D, 1);
mpz_init (T);
for(dw=0;(dw<(1U<<dwFactors)); dw++)
{
/* for all Mobius terms */
int iPower=B;
int iMobius=0;
unsigned dwIndex=0;
unsigned dwMask=1;
while(dwIndex < dwFactors)
{
if(dw&dwMask)
{
/* printf ("iMobius = %d iPower = %d, dwIndex = %d ", iMobius, iPower, dwIndex); */
iMobius++;
iPower/=factors[dwIndex];
/* printf ("Then iPower = %d\n", iPower); */
}
dwMask<<=1;
++dwIndex;
}
// gmp_fprintf (stderr, "Taking %Zd^%d-1\n", org_n, iPower);
mpz_pow_ui(T, org_n, iPower);
mpz_sub_ui(T, T, 1);
if(iMobius&1)
{
// gmp_fprintf (stderr, "Muling D=D*T %Zd*%Zd\n", D, T);
mpz_mul(D, D, T);
}
else
{
// gmp_fprintf (stderr, "Muling n=n*T %Zd*%Zd\n", n, T);
mpz_mul(n, n, T);
}
}
mpz_divexact(n, n, D);
mpz_clear(T);
mpz_clear(org_n);
mpz_clear(D);
return 1;
}
int aurif (mpz_t output, mpz_t n, mpz_t base, int sign) // Evaluate Aurifeullian polynomials
{
int b,k=mpz_get_ui(n);
mpz_t orig_base;
mpz_t C,D,l,m;
// Find a proper base
mpz_init_set(orig_base,base);
mpz_inits(C,D,l,m,NULL);
for(b=2;b<=11;b++)
{
mpz_set(base,orig_base);
mpz_mul_ui(base,base,b);
if(mpz_perfect_square_p(base)) break;
}
if(b==12) // not found
{
gmp_fprintf (stderr, "Error: base %Zd not supported for Aurifeullian factorization yet\n", orig_base);
return 0;
}
if(k%((b==5)?b:(2*b))!=0)
{
gmp_fprintf (stderr, "Error: exponent %Zd does not make sense for base %Zd\n", n, orig_base);
return 0;
}
k/=((b==5)?b:(2*b));
if(k%2==0)
{
gmp_fprintf (stderr, "Error: exponent %Zd does not make sense for base %Zd\n", n, orig_base);
return 0;
}
mpz_set(base,orig_base);
mpz_pow_ui(m, base, k);
mpz_mul_ui(l, m, b);
mpz_sqrt(l, l);
switch(b)
{
case 2:
case 3:
mpz_add_ui(C, m, 1);
mpz_set_ui(D, 1);
break;
case 5:
case 6:
mpz_add_ui(C, m, 3);
mpz_mul(C, C, m);
mpz_add_ui(C, C, 1);
mpz_add_ui(D, m, 1);
break;
case 7:
mpz_add_ui(C, m, 1);
mpz_pow_ui(C, C, 3);
mpz_add_ui(D, m, 1);
mpz_mul(D, D, m);
mpz_add_ui(D, D, 1);
break;
case 10:
mpz_add_ui(C, m, 5);
mpz_mul(C, C, m);
mpz_add_ui(C, C, 7);
mpz_mul(C, C, m);
mpz_add_ui(C, C, 5);
mpz_mul(C, C, m);
mpz_add_ui(C, C, 1);
mpz_add_ui(D, m, 2);
mpz_mul(D, D, m);
mpz_add_ui(D, D, 2);
mpz_mul(D, D, m);
mpz_add_ui(D, D, 1);
break;
case 11:
mpz_add_ui(C, m, 5);
mpz_mul(C, C, m);
mpz_sub_ui(C, C, 1);
mpz_mul(C, C, m);
mpz_sub_ui(C, C, 1);
mpz_mul(C, C, m);
mpz_add_ui(C, C, 5);
mpz_mul(C, C, m);
mpz_add_ui(C, C, 1);
mpz_add_ui(D, m, 1);
mpz_mul(D, D, m);
mpz_sub_ui(D, D, 1);
mpz_mul(D, D, m);
mpz_add_ui(D, D, 1);
mpz_mul(D, D, m);
mpz_add_ui(D, D, 1);
break;
default: // not supposed to arrive here
break;
}
mpz_set(output, C);
(sign>0 ? mpz_addmul : mpz_submul)(output, D, l);
// gmp_fprintf (stderr, "Calculated base=%Zd, exp=%Zd, C=%Zd, D=%Zd, output=%Zd\n",base,n,C,D,output);
mpz_clears(orig_base,C,D,l,m,NULL);
return 1;
}
int eval_PhiL (mpz_t *params, mpz_t n)
{
mpz_t aur;
int err1,err2;
mpz_init(aur);
err1=aurif(aur,params[0],n,-1);
err2=eval_Phi(params,n); // n now holds Phi(params[0],n)
mpz_gcd(n,n,aur);
mpz_clear(aur);
return err1*err2;
}
int eval_PhiM (mpz_t *params, mpz_t n)
{
mpz_t aur;
int err1,err2;
mpz_init(aur);
err1=aurif(aur,params[0],n,1);
err2=eval_Phi(params,n); // n now holds Phi(params[0],n)
mpz_gcd(n,n,aur);
mpz_clear(aur);
return err1*err2;
}
int eval_gcd (mpz_t *params, mpz_t n)
{
mpz_gcd(n, n, params[0]);
return 1;
}
int eval_U (mpz_t *params, mpz_t n)
/* params[0]=P, params[1]=Q */
{
unsigned long N;
mpz_t U1,U0,org_n,D,T; /* At each step U1 holds U(k), and U0 holds U(k-1) */
long k,l;
if (mpz_cmp_si (n, 0) < 0)
return 0;
if (mpz_cmp_ui (n, 1) == 0)
{
mpz_set_ui (n, 1);
return 1;
}
if (mpz_cmp_ui (n, 0) == 0)
{
mpz_set_ui (n, 0);
return 1;
}
if (mpz_fits_ulong_p (n) == 0)
return 0;
N = mpz_get_ui (n);
if (mpz_cmp_ui (params[0], 0) == 0)
{
if( N%2==0 )
{
mpz_set_ui (n, 0);
}
else
{
mpz_neg (params[1], params[1]);
mpz_pow_ui (n, params[1], (N-1)/2);
mpz_neg (params[1], params[1]);
}
return 1;
}
mpz_init_set (org_n, n);
mpz_init_set_ui (U1, 1);
mpz_init_set_ui (U0, 0);
mpz_init (D);
mpz_init (T);
mpz_mul (D, params[0], params[0]);
mpz_submul_ui (D, params[1], 4);
k=1;
for(l=mpz_sizeinbase(org_n,2)-2;l>=0;l--)
{
mpz_mul (U0, U0, U0);
mpz_mul (U1, U1, U1);
mpz_mul (U0, U0, params[1]);
mpz_sub (U0, U1, U0); // U(2k-1)=U(k)^2-QU(k-1)^2
mpz_pow_ui (T, params[1], k);
mpz_mul (U1, U1, D);
mpz_addmul_ui (U1, T, 2);
mpz_addmul (U1, params[1], U0); // U(2k+1)=DU(k)^2+2Q^k+QU(2k-1)
if (mpz_tstbit (org_n, l) )
{
k=2*k+1;
mpz_mul (U0,U0,params[1]); // U0 is 2k, U1 is 2k+1
mpz_add (U0,U1,U0);
mpz_divexact (U0,U0,params[0]);
}
else
{
k=2*k;
mpz_addmul (U1,U0,params[1]); // U0 is 2k-1, U1 is 2k
mpz_divexact (U1,U1,params[0]);
}
/* gmp_fprintf (stderr, "%d %Zd %Zd\n",k,U0,U1); */
}
mpz_set(n, U1);
mpz_clear(U0);
mpz_clear(U1);
mpz_clear(org_n);
mpz_clear(D);
mpz_clear(T);
return 1;
}
int eval_primU (mpz_t* params, mpz_t n)
{
int factors[200];
unsigned dwFactors=0, dw;
unsigned long N;
unsigned long p;
mpz_t D, T;
if (mpz_cmp_ui (n, 0) <= 0)
return 0;
if (mpz_cmp_ui (n, 1) == 0)
{
mpz_set_ui (n, 1);
return 1;
}
/* Ignore the special cases where P^2=0,Q or 4Q*/
if (mpz_cmp_ui (params[0], 0) == 0)
{
return 0;
}
mpz_init(D);
mpz_mul(D, params[0], params[0]);
if (mpz_cmp (D, params[1]) == 0)
{
return 0;
}
mpz_submul_ui(D, params[1], 4);
if (mpz_cmp_ui (D, 0) == 0)
{
return 0;
}
if (mpz_fits_ulong_p (n) == 0)
return 0;
N = mpz_get_ui (n);
/* Obtain the factors of N */
for (p = 2; p <= N; p++)
{
if (N % p == 0)
{
/* Add the factor one time */
factors[dwFactors++] = p;
/* but be sure to totally remove it */
do { N /= p; } while (N % p == 0);
}
}
N = mpz_get_ui (n);
mpz_set_ui (n, 1);
mpz_set_ui (D, 1);
mpz_init (T);
for(dw=0;(dw<(1U<<dwFactors)); dw++)
{
/* for all Mobius terms */
int iPower=N;
int iMobius=0;
unsigned dwIndex=0;
unsigned dwMask=1;
while(dwIndex < dwFactors)
{
if(dw&dwMask)
{
/* printf ("iMobius = %d iPower = %d, dwIndex = %d ", iMobius, iPower, dwIndex); */
iMobius++;
iPower/=factors[dwIndex];
/* printf ("Then iPower = %d\n", iPower); */
}
dwMask<<=1;
++dwIndex;
}
// gmp_fprintf (stderr, "Taking U(%Zd,%Zd,%d)\n", P,Q,iPower);
mpz_set_ui(T,iPower);
if(eval_U(params, T)==0)
{
return 0;
}
if(iMobius&1)
{
// gmp_fprintf (stderr, "Muling D=D*T %Zd*%Zd\n", D, T);
mpz_mul(D, D, T);
}
else
{
// gmp_fprintf (stderr, "Muling n=n*T %Zd*%Zd\n", n, T);
mpz_mul(n, n, T);
}
}
mpz_divexact(n, n, D);
mpz_clear(T);
mpz_clear(D);
return 1;
}
/* A simple partial-recursive decent parser */
int eval_2 (int bInFuncParams)
{
mpz_t n_stack[5];
mpz_t n;
mpz_t param_stack[5];
int i,j;
int num_base;
char op_stack[5];
char op;
char negate;
typedef int (*fptr)(mpz_t *,mpz_t);
const int num_of_funcs=6;
const char *func_names[]={"Phi","PhiL","PhiM","U","primU","gcd"};
const int func_num_params[]={2,2,2,3,3,2};
const fptr func_ptrs[]={eval_Phi,eval_PhiL,eval_PhiM,eval_U,eval_primU,eval_gcd};
char *paren_position;
char tentative_func_name[20];
int func_id;
for (i=0;i<5;i++)
{
op_stack[i]=0;
mpz_init(n_stack[i]);
mpz_init(param_stack[i]);
}
mpz_init(n);
op = 0;
negate = 0;
for (;;)
{
if ('-'==(*expr_str))
{
expr_str++;
negate=1;
}
else
negate=0;
if ('('==(*expr_str) || '['==(*expr_str) || '{'==(*expr_str)) /* Number is subexpression */
{
expr_str++;
eval_2 (bInFuncParams);
mpz_set(n, t);
}
else /* Number is decimal value */
{
for (i=0;isdigit((int) expr_str[i]);i++)
;
if (!i) /* No digits found */
{
/* check for a valid "function" */
paren_position=strchr(&expr_str[i], '(');
if (NULL==paren_position)
{
/* No parentheses found */
fprintf (stderr, "\nError - invalid number [%c]\n", expr_str[i]);
exit (EXIT_FAILURE);
}
strncpy(tentative_func_name,&expr_str[i],paren_position-&expr_str[i]);
tentative_func_name[paren_position-&expr_str[i]]='\0';
for (func_id=0;func_id<num_of_funcs;func_id++)
{
if (!strcasecmp (tentative_func_name, func_names[func_id]))
break;
}
if(func_id==num_of_funcs) /* No matching function name found */
{
fprintf (stderr, "Error, Unknown function %s()\n", tentative_func_name);
exit (EXIT_FAILURE);
}
/* Now we can actually process existing functions */
expr_str=paren_position+1;
for(j=0;j<func_num_params[func_id]-1;j++)
{
/* eval the first parameters. NOTE we pass a 1 since we ARE in parameter mode,
and this causes the ',' character to act as the end of expression */
if(eval_2 (1) != 2)
{
fprintf (stderr, "Error, Function %s() requires %d parameters\n", func_names[func_id], func_num_params[func_id]);
exit (EXIT_FAILURE);
}
mpz_set(param_stack[j], t);
}
/* Now eval the last parameter NOTE we pass a 0 since we are NOT expecting a ','
character to end the expression, but are expecting a ) character to end the function */
if (eval_2 (0))
{
mpz_set(n, t);
if( (func_ptrs[func_id])(param_stack, n) == 0 )
{
fprintf (stderr, "\nParsing Error - Invalid "
"parameter passed to the %s function\n", func_names[func_id]);
exit (EXIT_FAILURE);
}
}
goto MONADIC_SUFFIX_LOOP;
}
/* Now check for a hex number. If so, handle it as such */
num_base=10; /* assume base 10 */
if (i == 1 && !strncasecmp(expr_str, "0x", 2))
{
num_base = 16; /* Kick up to hex */
expr_str += 2; /* skip the 0x string of the number */
for (i=0;isxdigit((int) expr_str[i]);i++)
;
}
op=expr_str[i];
expr_str[i]=0;
mpz_set_str(n,expr_str,num_base);
expr_str+=i;
*expr_str=op;
}
if (negate)
mpz_neg(n,n);
/* This label is needed for "normal" primorials and factorials, since they are evaluated Monadic suffix
expressions. Most of this parser assumes Dyadic operators with the only exceptino being the
Monadic prefix operator of "unary minus" which is handled by simply ignoring it (but remembering),
and then fixing the expression up when completed. */
/* This is ALSO where functions should be sent. A function should "act" like a stand alone number.
We should NOT start processing, and expecting a number, but we should expect an operator first */
MONADIC_SUFFIX_LOOP:
op=*expr_str++;
if (0==op || ')'==op || ']'==op || '}'==op || (','==op&&bInFuncParams))
{
eval_power (n_stack[2],n,op_stack[2]);
eval_product (n_stack[1],n,op_stack[1]);
eval_sum (n_stack[0],n,op_stack[0]);
mpz_set(t, n);
mpz_clear(n);
for (i=0;i<5;i++)
{
mpz_clear(n_stack[i]);
mpz_clear(param_stack[i]);
}
/* Hurray! a valid expression (or sub-expression) was parsed! */
return ','==op?2:1;
}
else
{
if ('^' == op)
{
eval_power (n_stack[2],n,op_stack[2]);
mpz_set(n_stack[2], n);
op_stack[2]='^';
}
else if ('!' == op)
{
if (!isdigit((int) *expr_str))
{
/* If the next char is not a digit, then this is a simple factorial, and not a "multi" factorial */
mpz_set(n_stack[2], n);
op_stack[2]='!';
goto MONADIC_SUFFIX_LOOP;
}
eval_power (n_stack[2],n,op_stack[2]);
mpz_set(n_stack[2], n);
op_stack[2]='@';
}
else if ('#' == op)
{
if (!isdigit((int) *expr_str))
{
/* If the next char is not a digit, then this is a simple primorial, and not a "reduced" primorial */
mpz_set(n_stack[2], n);
op_stack[2]='#';
goto MONADIC_SUFFIX_LOOP;
}
eval_power (n_stack[2],n,op_stack[2]);
mpz_set(n_stack[2], n);
op_stack[2]='$';
}
else
{
if ('.'==op || '*'==op || '/'==op || '%'==op)
{
eval_power (n_stack[2],n,op_stack[2]);
op_stack[2]=0;
eval_product (n_stack[1],n,op_stack[1]);
mpz_set(n_stack[1], n);
op_stack[1]=op;
}
else
{
if ('+'==op || '-'==op)
{
eval_power (n_stack[2],n,op_stack[2]);
op_stack[2]=0;
eval_product (n_stack[1],n,op_stack[1]);
op_stack[1]=0;
eval_sum (n_stack[0],n,op_stack[0]);
mpz_set(n_stack[0], n);
op_stack[0]=op;
}
else /* Error - invalid operator */
{
fprintf (stderr, "\nError - unknown operator: '%c'\n", op);
exit (EXIT_FAILURE);
}
}
}
}
}
}
void init_expr(void)
{
mpz_init_set_ui(mpOne, 1);
}
void free_expr(void)
{
mpz_clear(mpOne);
}