Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - basemath - prime.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 20079-8a65571) Lines: 558 618 90.3 %
Date: 2017-01-18 05:50:33 Functions: 61 64 95.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : /*********************************************************************/
      17             : /**                                                                 **/
      18             : /**               PSEUDO PRIMALITY (MILLER-RABIN)                   **/
      19             : /**                                                                 **/
      20             : /*********************************************************************/
      21             : typedef struct {
      22             :   ulong n, sqrt1, sqrt2, t1, t;
      23             :   long r1;
      24             : } Fl_MR_Jaeschke_t;
      25             : 
      26             : typedef struct {
      27             :   GEN n, sqrt1, sqrt2, t1, t;
      28             :   long r1;
      29             : } MR_Jaeschke_t;
      30             : 
      31             : static void
      32       79902 : init_MR_Jaeschke(MR_Jaeschke_t *S, GEN n)
      33             : {
      34       79902 :   if (signe(n) < 0) n = absi(n);
      35       79902 :   S->n = n;
      36       79902 :   S->t = addsi(-1,n);
      37       79902 :   S->r1 = vali(S->t);
      38       79902 :   S->t1 = shifti(S->t, -S->r1);
      39       79902 :   S->sqrt1 = cgeti(lg(n)); S->sqrt1[1] = evalsigne(0)|evallgefint(2);
      40       79901 :   S->sqrt2 = cgeti(lg(n)); S->sqrt2[1] = evalsigne(0)|evallgefint(2);
      41       79901 : }
      42             : static void
      43     3572147 : Fl_init_MR_Jaeschke(Fl_MR_Jaeschke_t *S, ulong n)
      44             : {
      45     3572147 :   S->n = n;
      46     3572147 :   S->t = n-1;
      47     3572147 :   S->r1 = vals(S->t);
      48     3577008 :   S->t1 = S->t >> S->r1;
      49     3577008 :   S->sqrt1 = 0;
      50     3577008 :   S->sqrt2 = 0;
      51     3577008 : }
      52             : 
      53             : /* c = sqrt(-1) seen in bad_for_base. End-matching: compare or remember
      54             :  * If ends do mismatch, then we have factored n, and this information
      55             :  * should somehow be made available to the factoring machinery. But so
      56             :  * exceedingly rare... besides we use BSPW now. */
      57             : static int
      58        7028 : MR_Jaeschke_ok(MR_Jaeschke_t *S, GEN c)
      59             : {
      60        7028 :   if (signe(S->sqrt1))
      61             :   { /* saw one earlier: compare */
      62          59 :     if (!equalii(c, S->sqrt1) && !equalii(c, S->sqrt2))
      63             :     { /* too many sqrt(-1)s mod n */
      64           0 :       if (DEBUGLEVEL) {
      65           0 :         GEN z = gcdii(addii(c, S->sqrt1), S->n);
      66           0 :         pari_warn(warner,"found factor\n\t%Ps\ncurrently lost to the factoring machinery", z);
      67             :       }
      68           0 :       return 1;
      69             :     }
      70             :   } else { /* remember */
      71        6969 :     affii(c, S->sqrt1);
      72        6969 :     affii(subii(S->n, c), S->sqrt2);
      73             :   }
      74        7028 :   return 0;
      75             : }
      76             : static int
      77      975033 : Fl_MR_Jaeschke_ok(Fl_MR_Jaeschke_t *S, ulong c)
      78             : {
      79      975033 :   if (S->sqrt1)
      80             :   { /* saw one earlier: compare */
      81         508 :     if (c != S->sqrt1 && c != S->sqrt2) return 1;
      82             :   } else { /* remember */
      83      974525 :     S->sqrt1 = c;
      84      974525 :     S->sqrt2 = S->n - c;
      85             :   }
      86      975033 :   return 0;
      87             : }
      88             : 
      89             : /* is n strong pseudo-prime for base a ? 'End matching' (check for square
      90             :  * roots of -1) added by GN */
      91             : static int
      92       80029 : bad_for_base(MR_Jaeschke_t *S, GEN a)
      93             : {
      94       80029 :   pari_sp av = avma;
      95             :   long r;
      96       80029 :   GEN c2, c = Fp_pow(a, S->t1, S->n);
      97             : 
      98       80030 :   if (is_pm1(c) || equalii(S->t, c)) return 0;
      99             : 
     100             :   /* go fishing for -1, not for 1 (saves one squaring) */
     101      138854 :   for (r = S->r1 - 1; r; r--) /* r1 - 1 squarings */
     102             :   {
     103       74568 :     c2 = c; c = remii(sqri(c), S->n);
     104       74568 :     if (equalii(S->t, c)) return MR_Jaeschke_ok(S, c2);
     105       67540 :     if (gc_needed(av,1))
     106             :     {
     107           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"Rabin-Miller");
     108           0 :       c = gerepileuptoint(av, c);
     109             :     }
     110             :   }
     111       64286 :   return 1;
     112             : }
     113             : static int
     114     3577225 : Fl_bad_for_base(Fl_MR_Jaeschke_t *S, ulong a)
     115             : {
     116             :   long r;
     117     3577225 :   ulong c2, c = Fl_powu(a, S->t1, S->n);
     118             : 
     119     3585467 :   if (c == 1 || c == S->t) return 0;
     120             : 
     121             :   /* go fishing for -1, not for 1 (saves one squaring) */
     122     3791015 :   for (r = S->r1 - 1; r; r--) /* r1 - 1 squarings */
     123             :   {
     124     2536526 :     c2 = c; c = Fl_sqr(c, S->n);
     125     2536818 :     if (c == S->t) return Fl_MR_Jaeschke_ok(S, c2);
     126             :   }
     127     1254489 :   return 1;
     128             : }
     129             : 
     130             : /* Miller-Rabin test for k random bases */
     131             : long
     132          28 : millerrabin(GEN n, long k)
     133             : {
     134          28 :   pari_sp av2, av = avma;
     135             :   ulong r;
     136             :   long i;
     137             :   MR_Jaeschke_t S;
     138             : 
     139          28 :   if (typ(n) != t_INT) pari_err_TYPE("millerrabin",n);
     140          28 :   if (signe(n)<=0) return 0;
     141             :   /* If |n| <= 3, check if n = +- 1 */
     142          28 :   if (lgefint(n)==3 && uel(n,2)<=3) return uel(n,2) != 1;
     143             : 
     144          14 :   if (!mod2(n)) return 0;
     145           7 :   init_MR_Jaeschke(&S, n); av2 = avma;
     146          21 :   for (i=1; i<=k; i++)
     147             :   {
     148          20 :     do r = umodui(pari_rand(), n); while (!r);
     149          14 :     if (DEBUGLEVEL > 4) err_printf("Miller-Rabin: testing base %ld\n", r);
     150          14 :     if (bad_for_base(&S, utoipos(r))) { avma = av; return 0; }
     151          14 :     avma = av2;
     152             :   }
     153           7 :   avma = av; return 1;
     154             : }
     155             : 
     156             : GEN
     157          14 : gispseudoprime(GEN x, long flag)
     158          14 : { return flag? map_proto_lGL(millerrabin, x, flag): map_proto_lG(BPSW_psp,x); }
     159             : 
     160             : long
     161           0 : ispseudoprime(GEN x, long flag)
     162           0 : { return flag? millerrabin(x, flag): BPSW_psp(x); }
     163             : 
     164             : /* As above for k bases taken in pr (i.e not random). We must have |n|>2 and
     165             :  * 1<=k<=11 (not checked) or k in {16,17} to select some special sets of bases.
     166             :  *
     167             :  * From Jaeschke, 'On strong pseudoprimes to several bases', Math.Comp. 61
     168             :  * (1993), 915--926  (see also http://www.utm.edu/research/primes/prove2.html),
     169             :  * we have:
     170             :  *
     171             :  * k == 4  (bases 2,3,5,7)  detects all composites
     172             :  *    n <     118 670 087 467 == 172243 * 688969  with the single exception of
     173             :  *    n ==      3 215 031 751 == 151 * 751 * 28351,
     174             :  *
     175             :  * k == 5  (bases 2,3,5,7,11)  detects all composites
     176             :  *    n <   2 152 302 898 747 == 6763 * 10627 * 29947,
     177             :  *
     178             :  * k == 6  (bases 2,3,...,13)  detects all composites
     179             :  *    n <   3 474 749 660 383 == 1303 * 16927 * 157543,
     180             :  *
     181             :  * k == 7  (bases 2,3,...,17)  detects all composites
     182             :  *    n < 341 550 071 728 321 == 10670053 * 32010157,
     183             :  * Even this limiting value is caught by an end mismatch between bases 5 and 17
     184             :  *
     185             :  * Moreover, the four bases chosen at
     186             :  *
     187             :  * k == 16  (2,13,23,1662803)  detects all composites up
     188             :  * to at least 10^12, and the combination at
     189             :  *
     190             :  * k == 17  (31,73)  detects most odd composites without prime factors > 100
     191             :  * in the range  n < 2^36  (with less than 250 exceptions, indeed with fewer
     192             :  * than 1400 exceptions up to 2^42). --GN */
     193             : int
     194        1717 : Fl_MR_Jaeschke(ulong n, long k)
     195             : {
     196        1717 :   const ulong pr[] =
     197             :     { 0, 2,3,5,7,11,13,17,19,23,29, 31,73, 2,13,23,1662803UL, };
     198             :   const ulong *p;
     199             :   ulong r;
     200             :   long i;
     201             :   Fl_MR_Jaeschke_t S;
     202             : 
     203        1717 :   if (!(n & 1)) return 0;
     204        1717 :   if (k == 16)
     205             :   { /* use smaller (faster) bases if possible */
     206           0 :     p = (n < 3215031751UL)? pr: pr+13;
     207           0 :     k = 4;
     208             :   }
     209        1717 :   else if (k == 17)
     210             :   {
     211        1717 :     p = (n < 1373653UL)? pr: pr+11;
     212        1717 :     k = 2;
     213             :   }
     214           0 :   else p = pr; /* 2,3,5,... */
     215        1717 :   Fl_init_MR_Jaeschke(&S, n);
     216        5067 :   for (i=1; i<=k; i++)
     217             :   {
     218        3392 :     r = p[i] % n; if (!r) break;
     219        3392 :     if (Fl_bad_for_base(&S, r)) return 0;
     220             :   }
     221        1675 :   return 1;
     222             : }
     223             : 
     224             : int
     225        1852 : MR_Jaeschke(GEN n, long k)
     226             : {
     227        1852 :   pari_sp av2, av = avma;
     228        1852 :   const ulong pr[] =
     229             :     { 0, 2,3,5,7,11,13,17,19,23,29, 31,73, 2,13,23,1662803UL, };
     230             :   const ulong *p;
     231             :   long i;
     232             :   MR_Jaeschke_t S;
     233             : 
     234        1852 :   if (lgefint(n) == 3) return Fl_MR_Jaeschke(uel(n,2), k);
     235             : 
     236         135 :   if (!mod2(n)) return 0;
     237         135 :   if      (k == 16) { p = pr+13; k = 4; } /* 2,13,23,1662803 */
     238         135 :   else if (k == 17) { p = pr+11; k = 2; } /* 31,73 */
     239           0 :   else p = pr; /* 2,3,5,... */
     240         135 :   init_MR_Jaeschke(&S, n); av2 = avma;
     241         377 :   for (i=1; i<=k; i++)
     242             :   {
     243         256 :     if (bad_for_base(&S, utoipos(p[i]))) { avma = av; return 0; }
     244         242 :     avma = av2;
     245             :   }
     246         121 :   avma = av; return 1;
     247             : }
     248             : 
     249             : /*********************************************************************/
     250             : /**                                                                 **/
     251             : /**                      PSEUDO PRIMALITY (LUCAS)                   **/
     252             : /**                                                                 **/
     253             : /*********************************************************************/
     254             : /* compute n-th term of Lucas sequence modulo N.
     255             :  * v_{k+2} = P v_{k+1} - v_k, v_0 = 2, v_1 = P.
     256             :  * Assume n > 0 */
     257             : static GEN
     258       15488 : LucasMod(GEN n, ulong P, GEN N)
     259             : {
     260       15488 :   pari_sp av = avma;
     261       15488 :   GEN nd = int_MSW(n);
     262       15488 :   ulong m = *nd;
     263             :   long i, j;
     264       15488 :   GEN v = utoipos(P), v1 = utoipos(P*P - 2);
     265             : 
     266       15488 :   if (m == 1)
     267        1128 :     j = 0;
     268             :   else
     269             :   {
     270       14360 :     j = 1+bfffo(m); /* < BIL */
     271       14360 :     m <<= j; j = BITS_IN_LONG - j;
     272             :   }
     273       15488 :   for (i=lgefint(n)-2;;) /* cf. leftright_pow */
     274             :   {
     275     1026469 :     for (; j; m<<=1,j--)
     276             :     { /* v = v_k, v1 = v_{k+1} */
     277      998648 :       if (m&HIGHBIT)
     278             :       { /* set v = v_{2k+1}, v1 = v_{2k+2} */
     279      158979 :         v = subiu(mulii(v,v1), P);
     280      158979 :         v1= subiu(sqri(v1), 2);
     281             :       }
     282             :       else
     283             :       {/* set v = v_{2k}, v1 = v_{2k+1} */
     284      839669 :         v1= subiu(mulii(v,v1), P);
     285      839669 :         v = subiu(sqri(v), 2);
     286             :       }
     287      998648 :       v = modii(v, N);
     288      998648 :       v1= modii(v1,N);
     289      998648 :       if (gc_needed(av,1))
     290             :       {
     291           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"LucasMod");
     292           0 :         gerepileall(av, 2, &v,&v1);
     293             :       }
     294             :     }
     295       43309 :     if (--i == 0) return v;
     296       12333 :     j = BITS_IN_LONG;
     297       12333 :     nd=int_precW(nd);
     298       12333 :     m = *nd;
     299       12333 :   }
     300             : }
     301             : /* compute n-th term of Lucas sequence modulo N.
     302             :  * v_{k+2} = P v_{k+1} - v_k, v_0 = 2, v_1 = P.
     303             :  * Assume n > 0 */
     304             : static ulong
     305      437879 : u_LucasMod(ulong n, ulong P, ulong N)
     306             : {
     307             :   ulong v, v1, m;
     308             :   long j;
     309             : 
     310      437879 :   if (n == 1) return P;
     311      437860 :   j = 1 + bfffo(n); /* < BIL */
     312      437860 :   v = P; v1 = P*P - 2;
     313      437860 :   m = n<<j; j = BITS_IN_LONG - j;
     314    21847367 :   for (; j; m<<=1,j--)
     315             :   { /* v = v_k, v1 = v_{k+1} */
     316    21409507 :     if (m & HIGHBIT)
     317             :     { /* set v = v_{2k+1}, v1 = v_{2k+2} */
     318     2359511 :       v = Fl_sub(Fl_mul(v,v1,N), P, N);
     319     2359511 :       v1= Fl_sub(Fl_sqr(v1,N), 2UL, N);
     320             :     }
     321             :     else
     322             :     {/* set v = v_{2k}, v1 = v_{2k+1} */
     323    19049996 :       v1= Fl_sub(Fl_mul(v,v1,N),P, N);
     324    19049996 :       v = Fl_sub(Fl_sqr(v,N), 2UL, N);
     325             :     }
     326             :   }
     327      437860 :   return v;
     328             : }
     329             : 
     330             : int
     331      437886 : uislucaspsp(ulong n)
     332             : {
     333             :   long i, v;
     334      437886 :   ulong b, z, m = n + 1;
     335             : 
     336      975069 :   for (b=3, i=0;; b+=2, i++)
     337             :   {
     338      975069 :     ulong c = b*b - 4; /* = 1 mod 4 */
     339      975069 :     if (krouu(n % c, c) < 0) break;
     340      537190 :     if (i == 64 && uissquareall(n, &c)) return 0; /* oo loop if N = m^2 */
     341      537183 :   }
     342      437879 :   if (!m) return 0; /* neither 2^32-1 nor 2^64-1 are Lucas-pp */
     343      437879 :   v = vals(m); m >>= v;
     344      437879 :   z = u_LucasMod(m, b, n);
     345      437879 :   if (z == 2 || z == n-2) return 1;
     346      370111 :   for (i=1; i<v; i++)
     347             :   {
     348      370067 :     if (!z) return 1;
     349      189216 :     z = Fl_sub(Fl_sqr(z,n), 2UL, n);
     350      189216 :     if (z == 2) return 0;
     351             :   }
     352          44 :   return 0;
     353             : }
     354             : /* N > 3. Caller should check that N is not a square first (taken care of here,
     355             :  * but inefficient) */
     356             : static int
     357       15488 : IsLucasPsP(GEN N)
     358             : {
     359       15488 :   pari_sp av = avma;
     360             :   GEN m, z;
     361             :   long i, v;
     362             :   ulong b;
     363             : 
     364       34882 :   for (b=3;; b+=2)
     365             :   {
     366       34882 :     ulong c = b*b - 4; /* = 1 mod 4 */
     367       34882 :     if (b == 129 && Z_issquare(N)) return 0; /* avoid oo loop if N = m^2 */
     368       34882 :     if (krouu(umodiu(N,c), c) < 0) break;
     369       19394 :   }
     370       15488 :   m = addis(N,1); v = vali(m); m = shifti(m,-v);
     371       15488 :   z = LucasMod(m, b, N);
     372       15488 :   if (absequaliu(z, 2)) return 1;
     373       13526 :   if (equalii(z, subiu(N,2))) return 1;
     374       14638 :   for (i=1; i<v; i++)
     375             :   {
     376       14521 :     if (!signe(z)) return 1;
     377        8077 :     z = modii(subiu(sqri(z), 2), N);
     378        8077 :     if (absequaliu(z, 2)) return 0;
     379        8077 :     if (gc_needed(av,1))
     380             :     {
     381           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"IsLucasPsP");
     382           0 :       z = gerepileupto(av, z);
     383             :     }
     384             :   }
     385         117 :   return 0;
     386             : }
     387             : 
     388             : /* assume u odd, u > 1 */
     389             : static int
     390      272439 : iu_coprime(GEN N, ulong u)
     391             : {
     392      272439 :   const ulong n = umodiu(N, u);
     393      272439 :   return (n == 1 || gcduodd(n, u) == 1);
     394             : }
     395             : /* assume u odd, u > 1 */
     396             : static int
     397    17524341 : uu_coprime(ulong n, ulong u)
     398             : {
     399    17524341 :   return gcduodd(n, u) == 1;
     400             : }
     401             : 
     402             : /* composite strong 2-pseudoprime < 1016801 whose prime divisors are > 101 */
     403             : static int
     404     2195781 : is_2_prp_101(ulong n)
     405             : {
     406     2195781 :   switch(n) {
     407             :   case 42799:
     408             :   case 49141:
     409             :   case 88357:
     410             :   case 90751:
     411             :   case 104653:
     412             :   case 130561:
     413             :   case 196093:
     414             :   case 220729:
     415             :   case 253241:
     416             :   case 256999:
     417             :   case 271951:
     418             :   case 280601:
     419             :   case 357761:
     420             :   case 390937:
     421             :   case 458989:
     422             :   case 486737:
     423             :   case 489997:
     424             :   case 514447:
     425             :   case 580337:
     426             :   case 741751:
     427             :   case 838861:
     428             :   case 873181:
     429             :   case 877099:
     430             :   case 916327:
     431             :   case 976873:
     432         212 :   case 983401: return 1;
     433     2195569 :   } return 0;
     434             : }
     435             : 
     436             : static int
     437     3569521 : u_2_prp(ulong n)
     438             : {
     439             :   Fl_MR_Jaeschke_t S;
     440     3569521 :   Fl_init_MR_Jaeschke(&S, n);
     441     3576126 :   return Fl_bad_for_base(&S, 2) == 0;
     442             : }
     443             : static int
     444     1372321 : uBPSW_psp(ulong n) { return (u_2_prp(n) && uislucaspsp(n)); }
     445             : 
     446             : int
     447    17629472 : uisprime(ulong n)
     448             : {
     449    17629472 :   if (n < 103)
     450      863440 :     switch(n)
     451             :     {
     452             :       case 2:
     453             :       case 3:
     454             :       case 5:
     455             :       case 7:
     456             :       case 11:
     457             :       case 13:
     458             :       case 17:
     459             :       case 19:
     460             :       case 23:
     461             :       case 29:
     462             :       case 31:
     463             :       case 37:
     464             :       case 41:
     465             :       case 43:
     466             :       case 47:
     467             :       case 53:
     468             :       case 59:
     469             :       case 61:
     470             :       case 67:
     471             :       case 71:
     472             :       case 73:
     473             :       case 79:
     474             :       case 83:
     475             :       case 89:
     476             :       case 97:
     477      648849 :       case 101: return 1;
     478      214591 :       default: return 0;
     479             :     }
     480    16766032 :   if (!odd(n)) return 0;
     481             : #ifdef LONG_IS_64BIT
     482             :   /* 16294579238595022365 = 3*5*7*11*13*17*19*23*29*31*37*41*43*47*53
     483             :    *  7145393598349078859 = 59*61*67*71*73*79*83*89*97*101 */
     484    14212836 :   if (!uu_coprime(n, 16294579238595022365UL) ||
     485    10349816 :       !uu_coprime(n,  7145393598349078859UL)) return 0;
     486             : #else
     487             :   /* 4127218095 = 3*5*7*11*13*17*19*23*37
     488             :    * 3948078067 = 29*31*41*43*47*53
     489             :    * 4269855901 = 59*83*89*97*101
     490             :    * 1673450759 = 61*67*71*73*79 */
     491     2135525 :   if (!uu_coprime(n, 4127218095UL) ||
     492     1445422 :       !uu_coprime(n, 3948078067UL) ||
     493     1287768 :       !uu_coprime(n, 1673450759UL) ||
     494     1395892 :       !uu_coprime(n, 4269855901UL)) return 0;
     495             : #endif
     496     4580325 :   if (n < 10427) return 1;
     497     3524800 :   if (n < 1016801) return !is_2_prp_101(n) && u_2_prp(n);
     498     1333707 :   return uBPSW_psp(n);
     499             : }
     500             : 
     501             : /* assume no prime divisor <= 101 */
     502             : int
     503       16487 : uisprime_101(ulong n)
     504             : {
     505       16487 :   if (n < 10427) return 1;
     506       16474 :   if (n < 1016801) return !is_2_prp_101(n) && u_2_prp(n);
     507       12708 :   return uBPSW_psp(n);
     508             : }
     509             : 
     510             : /* assume no prime divisor <= 661 */
     511             : int
     512       25906 : uisprime_661(ulong n) { return uBPSW_psp(n); }
     513             : 
     514             : long
     515     4637687 : BPSW_psp(GEN N)
     516             : {
     517             :   pari_sp av;
     518             :   MR_Jaeschke_t S;
     519             :   int k;
     520             : 
     521     4637687 :   if (typ(N) != t_INT) pari_err_TYPE("BPSW_psp",N);
     522     4773211 :   if (signe(N) <= 0) return 0;
     523     4785699 :   if (lgefint(N) == 3) return uisprime(uel(N,2));
     524      117477 :   if (!mod2(N)) return 0;
     525             : #ifdef LONG_IS_64BIT
     526             :   /* 16294579238595022365 = 3*5*7*11*13*17*19*23*29*31*37*41*43*47*53
     527             :    *  7145393598349078859 = 59*61*67*71*73*79*83*89*97*101 */
     528      100753 :   if (!iu_coprime(N, 16294579238595022365UL) ||
     529       58672 :       !iu_coprime(N,  7145393598349078859UL)) return 0;
     530             : #else
     531             :   /* 4127218095 = 3*5*7*11*13*17*19*23*37
     532             :    * 3948078067 = 29*31*41*43*47*53
     533             :    * 4269855901 = 59*83*89*97*101
     534             :    * 1673450759 = 61*67*71*73*79 */
     535       99646 :   if (!iu_coprime(N, 4127218095UL) ||
     536       79250 :       !iu_coprime(N, 3948078067UL) ||
     537       72042 :       !iu_coprime(N, 1673450759UL) ||
     538       59117 :       !iu_coprime(N, 4269855901UL)) return 0;
     539             : #endif
     540             :   /* no prime divisor < 103 */
     541       75403 :   av = avma;
     542       75403 :   init_MR_Jaeschke(&S, N);
     543       75402 :   k = (!bad_for_base(&S, gen_2) && IsLucasPsP(N));
     544       75403 :   avma = av; return k;
     545             : }
     546             : 
     547             : /* can we write n = x^k ? Assume N has no prime divisor <= 2^14.
     548             :  * Not memory clean */
     549             : long
     550        7682 : isanypower_nosmalldiv(GEN N, GEN *px)
     551             : {
     552        7682 :   GEN x = N, y;
     553        7682 :   ulong mask = 7;
     554        7682 :   long ex, k = 1;
     555             :   forprime_t T;
     556        7682 :   while (Z_issquareall(x, &y)) { k <<= 1; x = y; }
     557        7682 :   while ( (ex = is_357_power(x, &y, &mask)) ) { k *= ex; x = y; }
     558        7682 :   (void)u_forprime_init(&T, 11, ULONG_MAX);
     559             :   /* stop when x^(1/k) < 2^14 */
     560        7682 :   while ( (ex = is_pth_power(x, &y, &T, 15)) ) { k *= ex; x = y; }
     561        7682 :   *px = x; return k;
     562             : }
     563             : 
     564             : /* no prime divisor <= 2^14 (> 661) */
     565             : long
     566       14471 : BPSW_psp_nosmalldiv(GEN N)
     567             : {
     568             :   pari_sp av;
     569             :   MR_Jaeschke_t S;
     570       14471 :   long l = lgefint(N);
     571             :   int k;
     572             : 
     573       14471 :   if (l == 3) return uisprime_661(uel(N,2));
     574        4378 :   av = avma;
     575             :   /* N large: test for pure power, rarely succeeds, but requires < 1% of
     576             :    * compositeness test times */
     577        4378 :   if (bit_accuracy(l) > 512 && isanypower_nosmalldiv(N, &N) != 1)
     578             :   {
     579          21 :     avma = av; return 0;
     580             :   }
     581        4357 :   init_MR_Jaeschke(&S, N);
     582        4357 :   k = (!bad_for_base(&S, gen_2) && IsLucasPsP(N));
     583        4357 :   avma = av; return k;
     584             : }
     585             : 
     586             : /***********************************************************************/
     587             : /**                                                                   **/
     588             : /**                       Pocklington-Lehmer                          **/
     589             : /**                        P-1 primality test                         **/
     590             : /**                                                                   **/
     591             : /***********************************************************************/
     592             : /* Assume x BPSW pseudoprime. Check whether it's small enough to be certified
     593             :  * prime (< 2^64). Reference for strong 2-pseudoprimes:
     594             :  *   http://www.cecm.sfu.ca/Pseudoprimes/index-2-to-64.html */
     595             : static int
     596      947543 : BPSW_isprime_small(GEN x)
     597             : {
     598      947543 :   long l = lgefint(x);
     599             : #ifdef LONG_IS_64BIT
     600      882327 :   return (l == 3);
     601             : #else
     602       65216 :   return (l <= 4);
     603             : #endif
     604             : }
     605             : 
     606             : /* Assume N > 1, p^e || N-1, p prime. Find a witness a(p) such that
     607             :  *   a^(N-1) = 1 (mod N)
     608             :  *   a^(N-1)/p - 1 invertible mod N.
     609             :  * Proves that any divisor of N is 1 mod p^e. Return 0 if N is composite */
     610             : static ulong
     611       15231 : pl831(GEN N, GEN p)
     612             : {
     613       15231 :   GEN b, c, g, Nmunp = diviiexact(addis(N,-1), p);
     614       15231 :   pari_sp av = avma;
     615             :   ulong a;
     616       22328 :   for(a = 2;; a++, avma = av)
     617             :   {
     618       22328 :     b = Fp_pow(utoipos(a), Nmunp, N);
     619       22328 :     if (!equali1(b)) break;
     620        7097 :   }
     621       15231 :   c = Fp_pow(b,p,N);
     622       15231 :   g = gcdii(addis(b,-1), N); /* 0 < g < N */
     623       15231 :   return (equali1(c) && equali1(g))? a: 0;
     624             : }
     625             : 
     626             : /* Brillhart, Lehmer, Selfridge test (Crandall & Pomerance, Th 4.1.5)
     627             :  * N^(1/3) <= f fully factored, f | N-1. If pl831(p) is true for
     628             :  * any prime divisor p of f, then any divisor of N is 1 mod f.
     629             :  * In that case return 1 iff N is prime */
     630             : static int
     631          63 : BLS_test(GEN N, GEN f)
     632             : {
     633             :   GEN c1, c2, r, q;
     634          63 :   q = dvmdii(N, f, &r);
     635          63 :   if (!is_pm1(r)) return 0;
     636          63 :   c2 = dvmdii(q, f, &c1);
     637             :   /* N = 1 + f c1 + f^2 c2, 0 <= c_i < f; check whether it is of the form
     638             :    * (1 + fa)(1 + fb) */
     639          63 :   return ! Z_issquare(subii(sqri(c1), shifti(c2,2)));
     640             : }
     641             : 
     642             : /* BPSW_psp(N) && !BPSW_isprime_small(N). Decide between Pocklington-Lehmer
     643             :  * and APRCL. Return a vector of (small) primes such that PL-witnesses
     644             :  * guarantee the primality of N. Return NULL if PL is likely too expensive.
     645             :  * Return gen_0 if BLS test finds N to be composite */
     646             : static GEN
     647        5008 : BPSW_try_PL(GEN N)
     648             : {
     649        5008 :   ulong B = minuu(1UL<<19, maxprime());
     650        5008 :   GEN E, p, U, F, N_1 = subiu(N,1);
     651        5008 :   GEN fa = Z_factor_limit(N_1, B), P = gel(fa,1);
     652        5008 :   long n = lg(P)-1;
     653             : 
     654        5008 :   p = gel(P,n);
     655             :   /* if p prime, then N-1 is fully factored */
     656        5008 :   if (cmpii(p,sqru(B)) <= 0 || (BPSW_psp_nosmalldiv(p) && BPSW_isprime(p)))
     657        3012 :     return P;
     658             : 
     659        1996 :   E = gel(fa,2);
     660        1996 :   U = powii(p, gel(E,n)); /* unfactored part of N-1 */
     661             :   /* factored part of N-1; n >= 2 since 2p | N-1 */
     662        1996 :   F = (n == 2)? powii(gel(P,1), gel(E,1)): diviiexact(N_1,  U);
     663        1996 :   setlg(P, n); /* remove last (composite) entry */
     664             : 
     665             :   /* N-1 = F U, F factored, U possibly composite, (U,F) = 1 */
     666        1996 :   if (cmpii(F, U) > 0) return P; /* 1/2-smooth */
     667        1989 :   if (cmpii(sqri(F), U) > 0) return BLS_test(N,F)? P: gen_0; /* 1/3-smooth */
     668        1933 :   return NULL; /* not smooth enough */
     669             : }
     670             : 
     671             : static GEN isprimePL(GEN N);
     672             : static GEN PL_certificate(GEN N, GEN F);
     673             : /* Assume N a BPSW pseudoprime. Return 0 if not prime, and a primality label
     674             :  * otherwise: 1 (small), 2 (APRCL), or PL-certificate  */
     675             : static GEN
     676          49 : check_prime(GEN N)
     677             : {
     678             :   GEN P;
     679          49 :   if (BPSW_isprime_small(N)) return gen_1;
     680             :   /* PL for small N: APRCL is faster but we prefer a certificate */
     681           0 :   if (expi(N) <= 250) return isprimePL(N);
     682           0 :   P = BPSW_try_PL(N);
     683             :   /* if PL likely too expensive: give up certificate and use APRCL */
     684           0 :   if (!P) return isprimeAPRCL(N)? gen_2: gen_0;
     685           0 :   return typ(P) != t_INT? PL_certificate(N,P): gen_0;
     686             : }
     687             : 
     688             : /* F is a vector whose entries are primes. For each of them, find a PL
     689             :  * witness. Return 0 if caller lied and F contains a composite */
     690             : static long
     691        3075 : PL_certify(GEN N, GEN F)
     692             : {
     693        3075 :   long i, l = lg(F);
     694       18250 :   for(i = 1; i < l; i++)
     695       15175 :     if (! pl831(N, gel(F,i))) return 0;
     696        3075 :   return 1;
     697             : }
     698             : /* F is a vector whose entries are *believed* to be primes. For each of them,
     699             :  * recording a witness and recursive primality certificate */
     700             : static GEN
     701          28 : PL_certificate(GEN N, GEN F)
     702             : {
     703          28 :   long i, l = lg(F);
     704          28 :   GEN W = cgetg(l,t_COL);
     705          28 :   GEN R = cgetg(l,t_COL);
     706          77 :   for(i=1; i<l; i++)
     707             :   {
     708          56 :     GEN p = gel(F,i);
     709          56 :     ulong witness = pl831(N,p);
     710          56 :     if (!witness) return gen_0;
     711          49 :     gel(W,i) = utoipos(witness);
     712          49 :     gel(R,i) = check_prime(p);
     713          49 :     if (isintzero(gel(R,i)))
     714             :     { /* composite in prime factorisation ! */
     715           0 :       err_printf("Not a prime: %Ps", p);
     716           0 :       pari_err_BUG("PL_certificate [false prime number]");
     717             :     }
     718             :   }
     719          21 :   return mkmat3(F, W, R);
     720             : }
     721             : /* Assume N is a strong BPSW pseudoprime, Pocklington-Lehmer primality proof.
     722             :  * Return gen_0 (non-prime), gen_1 (small prime), matrix (large prime)
     723             :  *
     724             :  * The matrix has 3 columns, [a,b,c] with
     725             :  * a[i] prime factor of N-1,
     726             :  * b[i] witness for a[i] as in pl831
     727             :  * c[i] check_prime(a[i]) */
     728             : static GEN
     729          35 : isprimePL(GEN N)
     730             : {
     731          35 :   pari_sp ltop = avma;
     732             :   GEN cbrtN, N_1, F, f;
     733             : 
     734          35 :   if (typ(N) != t_INT) pari_err_TYPE("isprimePL",N);
     735          35 :   switch(cmpis(N,2))
     736             :   {
     737           0 :     case -1:return gen_0;
     738           7 :     case 0: return gen_1;
     739             :   }
     740             :   /* N > 2 */
     741          28 :   cbrtN = sqrtnint(N, 3);
     742          28 :   N_1 = addis(N,-1);
     743          28 :   F = Z_factor_until(N_1, sqri(cbrtN));
     744          28 :   f = factorback(F); /* factored part of N-1, f^3 > N */
     745          28 :   if (DEBUGLEVEL>3)
     746             :   {
     747           0 :     GEN r = divri(itor(f,LOWDEFAULTPREC), N);
     748           0 :     err_printf("Pocklington-Lehmer: proving primality of N = %Ps\n", N);
     749           0 :     err_printf("Pocklington-Lehmer: N-1 factored up to %Ps! (%.3Ps%%)\n", f, r);
     750             :   }
     751             :   /* if N-1 is only N^(1/3)-smooth, BLS test */
     752          28 :   if (!equalii(f,N_1) && cmpii(sqri(f),N) <= 0 && !BLS_test(N,f))
     753           0 :   { avma = ltop; return gen_0; } /* Failed, N is composite */
     754          28 :   return gerepilecopy(ltop, PL_certificate(N, gel(F,1)));
     755             : }
     756             : 
     757             : /* assume N a BPSW pseudoprime, in particular, it is odd > 2. Prove N prime */
     758             : long
     759      947548 : BPSW_isprime(GEN N)
     760             : {
     761             :   pari_sp av;
     762             :   long t;
     763             :   GEN P;
     764      947548 :   if (BPSW_isprime_small(N)) return 1;
     765        5008 :   av = avma; P = BPSW_try_PL(N);
     766        5008 :   if (!P)
     767        1933 :     t = isprimeAPRCL(N); /* not smooth enough */
     768             :   else
     769        3075 :     t = (typ(P) == t_INT)? 0: PL_certify(N,P);
     770        5008 :   avma = av; return t;
     771             : }
     772             : 
     773             : GEN
     774     3481094 : gisprime(GEN x, long flag)
     775             : {
     776     3481094 :   switch (flag)
     777             :   {
     778     3481059 :     case 0: return map_proto_lG(isprime,x);
     779          21 :     case 1: return map_proto_G(isprimePL,x);
     780          14 :     case 2: return map_proto_lG(isprimeAPRCL,x);
     781             :   }
     782           0 :   pari_err_FLAG("gisprime");
     783           0 :   return NULL;
     784             : }
     785             : 
     786             : long
     787     4353658 : isprime(GEN x) { return BPSW_psp(x) && BPSW_isprime(x); }
     788             : 
     789             : /***********************************************************************/
     790             : /**                                                                   **/
     791             : /**                          PRIME NUMBERS                            **/
     792             : /**                                                                   **/
     793             : /***********************************************************************/
     794             : 
     795             : static struct {
     796             :   ulong p;
     797             :   long n;
     798             : } prime_table[] = {
     799             :   {           0,          0},
     800             :   {        7919,       1000},
     801             :   {       17389,       2000},
     802             :   {       27449,       3000},
     803             :   {       37813,       4000},
     804             :   {       48611,       5000},
     805             :   {       59359,       6000},
     806             :   {       70657,       7000},
     807             :   {       81799,       8000},
     808             :   {       93179,       9000},
     809             :   {      104729,      10000},
     810             :   {      224737,      20000},
     811             :   {      350377,      30000},
     812             :   {      479909,      40000},
     813             :   {      611953,      50000},
     814             :   {      746773,      60000},
     815             :   {      882377,      70000},
     816             :   {     1020379,      80000},
     817             :   {     1159523,      90000},
     818             :   {     1299709,     100000},
     819             :   {     2750159,     200000},
     820             :   {     7368787,     500000},
     821             :   {    15485863,    1000000},
     822             :   {    32452843,    2000000},
     823             :   {    86028121,    5000000},
     824             :   {   179424673,   10000000},
     825             :   {   373587883,   20000000},
     826             :   {   982451653,   50000000},
     827             :   {  2038074743,  100000000},
     828             :   {  4000000483UL,189961831},
     829             :   {  4222234741UL,200000000},
     830             : #if BITS_IN_LONG == 64
     831             :   { 5336500537UL,   250000000L},
     832             :   { 6461335109UL,   300000000L},
     833             :   { 7594955549UL,   350000000L},
     834             :   { 8736028057UL,   400000000L},
     835             :   { 9883692017UL,   450000000L},
     836             :   { 11037271757UL,  500000000L},
     837             :   { 13359555403UL,  600000000L},
     838             :   { 15699342107UL,  700000000L},
     839             :   { 18054236957UL,  800000000L},
     840             :   { 20422213579UL,  900000000L},
     841             :   { 22801763489UL, 1000000000L},
     842             :   { 47055833459UL, 2000000000L},
     843             :   { 71856445751UL, 3000000000L},
     844             :   { 97011687217UL, 4000000000L},
     845             :   {122430513841UL, 5000000000L},
     846             :   {148059109201UL, 6000000000L},
     847             :   {173862636221UL, 7000000000L},
     848             :   {200000000507UL, 8007105083L},
     849             :   {225898512559UL, 9000000000L},
     850             :   {252097800623UL,10000000000L},
     851             :   {384489816343UL,15000000000L},
     852             :   {518649879439UL,20000000000L},
     853             :   {654124187867UL,25000000000L},
     854             :   {790645490053UL,30000000000L},
     855             :   {928037044463UL,35000000000L},
     856             :  {1066173339601UL,40000000000L},
     857             :  {1344326694119UL,50000000000L},
     858             :  {1624571841097UL,60000000000L},
     859             :  {1906555030411UL,70000000000L},
     860             :  {2190026988349UL,80000000000L},
     861             :  {2474799787573UL,90000000000L},
     862             :  {2760727302517UL,100000000000L}
     863             : #endif
     864             : };
     865             : static const int prime_table_len = numberof(prime_table);
     866             : 
     867             : /* find prime closest to n in prime_table. */
     868             : static long
     869    12163471 : prime_table_closest_p(ulong n)
     870             : {
     871             :   long i;
     872    12501339 :   for (i = 1; i < prime_table_len; i++)
     873             :   {
     874    12501323 :     ulong p = prime_table[i].p;
     875    12501323 :     if (p > n)
     876             :     {
     877    12163455 :       ulong u = n - prime_table[i-1].p;
     878    12163455 :       if (p - n > u) i--;
     879    12163455 :       break;
     880             :     }
     881             :   }
     882    12163471 :   if (i == prime_table_len) i = prime_table_len - 1;
     883    12163471 :   return i;
     884             : }
     885             : 
     886             : /* return the n-th successor of prime p > 2 */
     887             : static GEN
     888          70 : prime_successor(ulong p, ulong n)
     889             : {
     890             :   forprime_t S;
     891             :   ulong i;
     892          70 :   forprime_init(&S, utoipos(p+1), NULL);
     893          70 :   for (i = 1; i < n; i++) (void)forprime_next(&S);
     894          70 :   return forprime_next(&S);
     895             : }
     896             : /* find the N-th prime */
     897             : static GEN
     898         161 : prime_table_find_n(ulong N)
     899             : {
     900             :   byteptr d;
     901         161 :   ulong n, p, maxp = maxprime();
     902             :   long i;
     903        2037 :   for (i = 1; i < prime_table_len; i++)
     904             :   {
     905        2037 :     n = prime_table[i].n;
     906        2037 :     if (n > N)
     907             :     {
     908         161 :       ulong u = N - prime_table[i-1].n;
     909         161 :       if (n - N > u) i--;
     910         161 :       break;
     911             :     }
     912             :   }
     913         161 :   if (i == prime_table_len) i = prime_table_len - 1;
     914         161 :   p = prime_table[i].p;
     915         161 :   n = prime_table[i].n;
     916         161 :   if (n > N && p > maxp)
     917             :   {
     918          14 :     i--;
     919          14 :     p = prime_table[i].p;
     920          14 :     n = prime_table[i].n;
     921             :   }
     922             :   /* if beyond prime table, then n <= N */
     923         161 :   d = diffptr + n;
     924         161 :   if (n > N)
     925             :   {
     926          14 :     n -= N;
     927       50624 :     do { n--; PREC_PRIME_VIADIFF(p,d); } while (n) ;
     928             :   }
     929         147 :   else if (n < N)
     930             :   {
     931         147 :     n = N-n;
     932         147 :     if (p > maxp) return prime_successor(p, n);
     933             :     do {
     934       44492 :       if (!*d) return prime_successor(p, n);
     935       44492 :       n--; NEXT_PRIME_VIADIFF(p,d);
     936       44492 :     } while (n) ;
     937             :   }
     938          91 :   return utoipos(p);
     939             : }
     940             : 
     941             : ulong
     942           0 : uprime(long N)
     943             : {
     944           0 :   pari_sp av = avma;
     945             :   GEN p;
     946           0 :   if (N <= 0) pari_err_DOMAIN("prime", "n", "<=",gen_0, stoi(N));
     947           0 :   p = prime_table_find_n(N);
     948           0 :   if (lgefint(p) != 3) pari_err_OVERFLOW("uprime");
     949           0 :   avma = av; return p[2];
     950             : }
     951             : GEN
     952         168 : prime(long N)
     953             : {
     954         168 :   pari_sp av = avma;
     955             :   GEN p;
     956         168 :   if (N <= 0) pari_err_DOMAIN("prime", "n", "<=",gen_0, stoi(N));
     957         161 :   new_chunk(4); /*HACK*/
     958         161 :   p = prime_table_find_n(N);
     959         161 :   avma = av; return icopy(p);
     960             : }
     961             : 
     962             : /* random b-bit prime */
     963             : GEN
     964          49 : randomprime(GEN N)
     965             : {
     966          49 :   pari_sp av = avma, av2;
     967             :   GEN a, b, d;
     968          49 :   if (!N)
     969             :     for(;;)
     970             :     {
     971          63 :       ulong p = random_bits(31);
     972          63 :       if (uisprime(p)) return utoipos(p);
     973          56 :     }
     974          42 :   switch(typ(N))
     975             :   {
     976             :     case t_INT:
     977          14 :       a = gen_2;
     978          14 :       b = subiu(N,1); /* between 2 and N-1 */
     979          14 :       d = subiu(N,2);
     980          14 :       if (signe(d) <= 0)
     981           7 :         pari_err_DOMAIN("randomprime","N", "<=", gen_2, N);
     982           7 :       break;
     983             :     case t_VEC:
     984          28 :       if (lg(N) != 3) pari_err_TYPE("randomprime",N);
     985          28 :       a = gel(N,1);
     986          28 :       b = gel(N,2);
     987          28 :       if (gcmp(b, a) < 0)
     988           7 :         pari_err_DOMAIN("randomprime","b-a", "<", gen_0, mkvec2(a,b));
     989          21 :       if (typ(a) != t_INT)
     990             :       {
     991           7 :         a = gceil(a);
     992           7 :         if (typ(a) != t_INT) pari_err_TYPE("randomprime",a);
     993             :       }
     994          21 :       if (typ(b) != t_INT)
     995             :       {
     996           7 :         b = gfloor(b);
     997           7 :         if (typ(b) != t_INT) pari_err_TYPE("randomprime",b);
     998             :       }
     999          21 :       if (cmpis(a, 2) < 0)
    1000             :       {
    1001           7 :         a = gen_2;
    1002           7 :         d = subiu(b,1);
    1003             :       }
    1004             :       else
    1005          14 :         d = addiu(subii(b,a), 1);
    1006          21 :       if (signe(d) <= 0)
    1007          14 :         pari_err_DOMAIN("randomprime","floor(b) - max(ceil(a),2)", "<",
    1008             :                         gen_0, mkvec2(a,b));
    1009           7 :       break;
    1010             :     default:
    1011           0 :       pari_err_TYPE("randomprime", N);
    1012             :       return NULL; /*LCOV_EXCL_LINE*/
    1013             :   }
    1014          14 :   av2 = avma;
    1015             :   for (;;)
    1016             :   {
    1017         210 :     GEN p = addii(a, randomi(d));
    1018         210 :     if (BPSW_psp(p)) return gerepileuptoint(av, p);
    1019         196 :     avma = av2;
    1020         196 :   }
    1021             : }
    1022             : 
    1023             : /* set *pp = nextprime(a) = p
    1024             :  *     *pd so that NEXT_PRIME_VIADIFF(d, p) = nextprime(p+1)
    1025             :  *     *pn so that p = the n-th prime
    1026             :  * error if nextprime(a) is out of primetable bounds */
    1027             : void
    1028    12163348 : prime_table_next_p(ulong a, byteptr *pd, ulong *pp, ulong *pn)
    1029             : {
    1030             :   byteptr d;
    1031    12163348 :   ulong p, n, maxp = maxprime();
    1032    12163300 :   long i = prime_table_closest_p(a);
    1033    12163261 :   p = prime_table[i].p;
    1034    12163261 :   if (p > a && p > maxp)
    1035             :   {
    1036           0 :     i--;
    1037           0 :     p = prime_table[i].p;
    1038             :   }
    1039             :   /* if beyond prime table, then p <= a */
    1040    12163261 :   n = prime_table[i].n;
    1041    12163261 :   d = diffptr + n;
    1042    12163261 :   if (p < a)
    1043             :   {
    1044    12055428 :     if (a > maxp) pari_err_MAXPRIME(a);
    1045    27059470 :     do { n++; NEXT_PRIME_VIADIFF(p,d); } while (p < a);
    1046             :   }
    1047      107833 :   else if (p != a)
    1048             :   {
    1049     2372539 :     do { n--; PREC_PRIME_VIADIFF(p,d); } while (p > a) ;
    1050        8669 :     if (p < a) { NEXT_PRIME_VIADIFF(p,d); n++; }
    1051             :   }
    1052    12163313 :   *pn = n;
    1053    12163313 :   *pp = p;
    1054    12163313 :   *pd = d;
    1055    12163313 : }
    1056             : 
    1057             : ulong
    1058        9274 : uprimepi(ulong a)
    1059             : {
    1060        9274 :   ulong p, n, maxp = maxprime();
    1061        9274 :   if (a <= maxp)
    1062             :   {
    1063             :     byteptr d;
    1064        9149 :     prime_table_next_p(a, &d, &p, &n);
    1065        9149 :     return p == a? n: n-1;
    1066             :   }
    1067             :   else
    1068             :   {
    1069         125 :     long i = prime_table_closest_p(a);
    1070             :     forprime_t S;
    1071         125 :     p = prime_table[i].p;
    1072         125 :     if (p > a)
    1073             :     {
    1074          28 :       i--;
    1075          28 :       p = prime_table[i].p;
    1076             :     }
    1077             :     /* p = largest prime in table <= a */
    1078         125 :     n = prime_table[i].n;
    1079         125 :     (void)u_forprime_init(&S, p+1, a);
    1080         125 :     for (; p; n++) p = u_forprime_next(&S);
    1081         125 :     return n-1;
    1082             :   }
    1083             : }
    1084             : 
    1085             : GEN
    1086         252 : primepi(GEN x)
    1087             : {
    1088         252 :   pari_sp av = avma;
    1089         252 :   GEN pp, nn, N = typ(x) == t_INT? x: gfloor(x);
    1090             :   forprime_t S;
    1091             :   ulong n, p;
    1092             :   long i, l;
    1093         252 :   if (typ(N) != t_INT) pari_err_TYPE("primepi",N);
    1094         252 :   if (signe(N) <= 0) return gen_0;
    1095         252 :   avma = av; l = lgefint(N);
    1096         252 :   if (l == 3) return utoi(uprimepi(N[2]));
    1097           1 :   i = prime_table_len-1;
    1098           1 :   p = prime_table[i].p;
    1099           1 :   n = prime_table[i].n;
    1100           1 :   (void)forprime_init(&S, utoipos(p+1), N);
    1101           1 :   nn = setloop(utoipos(n));
    1102           1 :   pp = gen_0;
    1103           1 :   for (; pp; incloop(nn)) pp = forprime_next(&S);
    1104           1 :   return gerepileuptoint(av, subiu(nn,1));
    1105             : }
    1106             : 
    1107             : /* pi(x) < x/log x * (1 + 1/log x + 2.51/log^2 x)), x>=355991 [ Dusart ]
    1108             :  * pi(x) < x/(log x - 1.1), x >= 60184 [ Dusart ]
    1109             :  * ? \p9
    1110             :  * ? M = 0; for(x = 4, 60184, M = max(M, log(x) - x/primepi(x))); M
    1111             :  * %1 = 1.11196252 */
    1112             : double
    1113       54657 : primepi_upper_bound(double x)
    1114             : {
    1115       54657 :   if (x >= 355991)
    1116             :   {
    1117          14 :     double L = 1/log(x);
    1118          14 :     return x * L * (1 + L + 2.51*L*L);
    1119             :   }
    1120       54643 :   if (x >= 60184) return x / (log(x) - 1.1);
    1121       54643 :   if (x < 5) return 2; /* don't bother */
    1122       50877 :   return x / (log(x) - 1.111963);
    1123             : }
    1124             : /* pi(x) > x/log x (1 + 1/log x), x >= 599 [ Dusart ]
    1125             :  * pi(x) > x / (log x + 2), x >= 55 [ Rosser ] */
    1126             : double
    1127          14 : primepi_lower_bound(double x)
    1128             : {
    1129          14 :   if (x >= 599)
    1130             :   {
    1131          14 :     double L = 1/log(x);
    1132          14 :     return x * L * (1 + L);
    1133             :   }
    1134           0 :   if (x < 55) return 0; /* don't bother */
    1135           0 :   return x / (log(x) + 2.);
    1136             : }
    1137             : GEN
    1138           1 : gprimepi_upper_bound(GEN x)
    1139             : {
    1140           1 :   pari_sp av = avma;
    1141             :   double L;
    1142           1 :   if (typ(x) != t_INT) x = gfloor(x);
    1143           1 :   if (expi(x) <= 1022)
    1144             :   {
    1145           1 :     avma = av;
    1146           1 :     return dbltor(primepi_upper_bound(gtodouble(x)));
    1147             :   }
    1148           0 :   x = itor(x, LOWDEFAULTPREC);
    1149           0 :   L = 1 / rtodbl(logr_abs(x));
    1150           0 :   x = mulrr(x, dbltor(L * (1 + L + 2.51*L*L)));
    1151           0 :   return gerepileuptoleaf(av, x);
    1152             : }
    1153             : GEN
    1154           1 : gprimepi_lower_bound(GEN x)
    1155             : {
    1156           1 :   pari_sp av = avma;
    1157             :   double L;
    1158           1 :   if (typ(x) != t_INT) x = gfloor(x);
    1159           1 :   if (abscmpiu(x, 55) <= 0) return gen_0;
    1160           1 :   if (expi(x) <= 1022)
    1161             :   {
    1162           1 :     avma = av;
    1163           1 :     return dbltor(primepi_lower_bound(gtodouble(x)));
    1164             :   }
    1165           0 :   x = itor(x, LOWDEFAULTPREC);
    1166           0 :   L = 1 / rtodbl(logr_abs(x));
    1167           0 :   x = mulrr(x, dbltor(L * (1 + L)));
    1168           0 :   return gerepileuptoleaf(av, x);
    1169             : }
    1170             : 
    1171             : GEN
    1172          63 : primes(long n)
    1173             : {
    1174             :   forprime_t S;
    1175             :   long i;
    1176             :   GEN y;
    1177          63 :   if (n <= 0) return cgetg(1, t_VEC);
    1178          63 :   y = cgetg(n+1, t_VEC);
    1179          63 :   (void)new_chunk(3*n); /*HACK*/
    1180          63 :   u_forprime_init(&S, 2, ULONG_MAX);
    1181          63 :   avma = (pari_sp)y;
    1182          63 :   for (i = 1; i <= n; i++) gel(y, i) = utoipos( u_forprime_next(&S) );
    1183          63 :   return y;
    1184             : }
    1185             : GEN
    1186           0 : primes_zv(long n)
    1187             : {
    1188             :   forprime_t S;
    1189             :   long i;
    1190             :   GEN y;
    1191           0 :   if (n <= 0) return cgetg(1, t_VECSMALL);
    1192           0 :   y = cgetg(n+1,t_VECSMALL);
    1193           0 :   u_forprime_init(&S, 2, ULONG_MAX);
    1194           0 :   for (i = 1; i <= n; i++) y[i] =  u_forprime_next(&S);
    1195           0 :   avma = (pari_sp)y; return y;
    1196             : }
    1197             : GEN
    1198         119 : primes0(GEN N)
    1199             : {
    1200         119 :   switch(typ(N))
    1201             :   {
    1202          63 :     case t_INT: return primes(itos(N));
    1203             :     case t_VEC:
    1204          56 :       if (lg(N) == 3) return primes_interval(gel(N,1),gel(N,2));
    1205             :   }
    1206           0 :   pari_err_TYPE("primes", N);
    1207           0 :   return NULL;
    1208             : }
    1209             : 
    1210             : GEN
    1211          56 : primes_interval(GEN a, GEN b)
    1212             : {
    1213          56 :   pari_sp av = avma;
    1214             :   forprime_t S;
    1215             :   long i, n;
    1216             :   GEN y, d, p;
    1217          56 :   if (typ(a) != t_INT)
    1218             :   {
    1219           0 :     a = gceil(a);
    1220           0 :     if (typ(a) != t_INT) pari_err_TYPE("primes_interval",a);
    1221             :   }
    1222          56 :   if (typ(b) != t_INT)
    1223             :   {
    1224           7 :     b = gfloor(b);
    1225           7 :     if (typ(b) != t_INT) pari_err_TYPE("primes_interval",b);
    1226             :   }
    1227          49 :   if (signe(a) < 0) a = gen_2;
    1228          49 :   d = subii(b, a);
    1229          49 :   if (signe(d) < 0 || signe(b) <= 0) { avma = av; return cgetg(1, t_VEC); }
    1230          49 :   if (lgefint(b) == 3)
    1231             :   {
    1232          33 :     avma = av;
    1233          33 :     y = primes_interval_zv(itou(a), itou(b));
    1234          33 :     n = lg(y); settyp(y, t_VEC);
    1235          33 :     for (i = 1; i < n; i++) gel(y,i) = utoipos(y[i]);
    1236          33 :     return y;
    1237             :   }
    1238             :   /* at most d+1 primes in [a,b]. If d large, try better bound to lower
    1239             :    * memory use */
    1240          16 :   if (abscmpiu(d,100000) > 0)
    1241             :   {
    1242           1 :     GEN D = gsub(gprimepi_upper_bound(b), gprimepi_lower_bound(a));
    1243           1 :     D = ceil_safe(D);
    1244           1 :     if (cmpii(D, d) < 0) d = D;
    1245             :   }
    1246          16 :   n = itos(d)+1;
    1247          16 :   forprime_init(&S, a, b);
    1248          16 :   y = cgetg(n+1, t_VEC); i = 1;
    1249          16 :   while ((p = forprime_next(&S))) gel(y, i++) = icopy(p);
    1250          16 :   setlg(y, i); return gerepileupto(av, y);
    1251             : }
    1252             : 
    1253             : /* a <= b, at most d primes in [a,b]. Return them */
    1254             : static GEN
    1255         152 : primes_interval_i(ulong a, ulong b, ulong d)
    1256             : {
    1257         152 :   ulong p, i = 1, n = d + 1;
    1258             :   forprime_t S;
    1259         152 :   GEN y = cgetg(n+1, t_VECSMALL);
    1260         152 :   pari_sp av = avma;
    1261         152 :   u_forprime_init(&S, a, b);
    1262         152 :   while ((p = u_forprime_next(&S))) y[i++] = p;
    1263         152 :   avma = av; setlg(y, i); stackdummy((pari_sp)(y + i), (pari_sp)(y + n+1));
    1264         152 :   return y;
    1265             : }
    1266             : GEN
    1267          33 : primes_interval_zv(ulong a, ulong b)
    1268             : {
    1269             :   ulong d;
    1270          33 :   if (!a) return primes_upto_zv(b);
    1271          33 :   if (b < a) return cgetg(1, t_VECSMALL);
    1272          33 :   d = b - a;
    1273          33 :   if (d > 100000UL)
    1274             :   {
    1275          13 :     ulong D = (ulong)ceil(primepi_upper_bound(b)-primepi_lower_bound(a));
    1276          13 :     if (D < d) d = D;
    1277             :   }
    1278          33 :   return primes_interval_i(a, b, d);
    1279             : }
    1280             : GEN
    1281         119 : primes_upto_zv(ulong b)
    1282             : {
    1283             :   ulong d;
    1284         119 :   if (b < 2) return cgetg(1, t_VECSMALL);
    1285         119 :   d = (b > 100000UL)? (ulong)primepi_upper_bound(b): b;
    1286         119 :   return primes_interval_i(2, b, d);
    1287             : }
    1288             : 
    1289             : /***********************************************************************/
    1290             : /**                                                                   **/
    1291             : /**                       PRIVATE PRIME TABLE                         **/
    1292             : /**                                                                   **/
    1293             : /***********************************************************************/
    1294             : /* delete dummy NULL entries */
    1295             : static void
    1296          21 : cleanprimetab(GEN T)
    1297             : {
    1298          21 :   long i,j, l = lg(T);
    1299          70 :   for (i = j = 1; i < l; i++)
    1300          49 :     if (T[i]) T[j++] = T[i];
    1301          21 :   setlg(T,j);
    1302          21 : }
    1303             : /* remove p from T */
    1304             : static void
    1305          28 : rmprime(GEN T, GEN p)
    1306             : {
    1307             :   long i;
    1308          28 :   if (typ(p) != t_INT) pari_err_TYPE("removeprimes",p);
    1309          28 :   i = ZV_search(T, p);
    1310          28 :   if (!i)
    1311           7 :     pari_err_DOMAIN("removeprime","prime","not in",
    1312             :                     strtoGENstr("primetable"), p);
    1313          21 :   gunclone(gel(T,i)); gel(T,i) = NULL;
    1314          21 :   cleanprimetab(T);
    1315          21 : }
    1316             : 
    1317             : /*stolen from ZV_union_shallow() : clone entries from y */
    1318             : static GEN
    1319          28 : addp_union(GEN x, GEN y)
    1320             : {
    1321          28 :   long i, j, k, lx = lg(x), ly = lg(y);
    1322          28 :   GEN z = cgetg(lx + ly - 1, t_VEC);
    1323          28 :   i = j = k = 1;
    1324          63 :   while (i<lx && j<ly)
    1325             :   {
    1326           7 :     int s = cmpii(gel(x,i), gel(y,j));
    1327           7 :     if (s < 0)
    1328           0 :       gel(z,k++) = gel(x,i++);
    1329           7 :     else if (s > 0)
    1330           0 :       gel(z,k++) = gclone(gel(y,j++));
    1331             :     else {
    1332           7 :       gel(z,k++) = gel(x,i++);
    1333           7 :       j++;
    1334             :     }
    1335             :   }
    1336          28 :   while (i<lx) gel(z,k++) = gel(x,i++);
    1337          28 :   while (j<ly) gel(z,k++) = gclone(gel(y,j++));
    1338          28 :   setlg(z, k); return z;
    1339             : }
    1340             : 
    1341             : /* p is NULL, or a single element or a row vector with "primes" to add to
    1342             :  * prime table. */
    1343             : static GEN
    1344         161 : addp(GEN *T, GEN p)
    1345             : {
    1346         161 :   pari_sp av = avma;
    1347             :   long i, l;
    1348             :   GEN v;
    1349             : 
    1350         161 :   if (!p || lg(p) == 1) return *T;
    1351          42 :   if (!is_vec_t(typ(p))) p = mkvec(p);
    1352             : 
    1353          42 :   RgV_check_ZV(p, "addprimes");
    1354          35 :   v = gen_indexsort_uniq(p, (void*)&cmpii, &cmp_nodata);
    1355          35 :   p = vecpermute(p, v);
    1356          35 :   if (abscmpiu(gel(p,1), 2) < 0) pari_err_DOMAIN("addprimes", "p", "<", gen_2,p);
    1357          28 :   p = addp_union(*T, p);
    1358          28 :   l = lg(p);
    1359          28 :   if (l != lg(*T))
    1360             :   {
    1361          28 :     GEN old = *T, t = cgetalloc(t_VEC, l);
    1362          28 :     for (i = 1; i < l; i++) gel(t,i) = gel(p,i);
    1363          28 :     *T = t; free(old);
    1364             :   }
    1365          28 :   avma = av; return *T;
    1366             : }
    1367             : GEN
    1368         161 : addprimes(GEN p) { return addp(&primetab, p); }
    1369             : 
    1370             : static GEN
    1371          28 : rmprimes(GEN T, GEN prime)
    1372             : {
    1373             :   long i,tx;
    1374             : 
    1375          28 :   if (!prime) return T;
    1376          28 :   tx = typ(prime);
    1377          28 :   if (is_vec_t(tx))
    1378             :   {
    1379          14 :     if (prime == T)
    1380             :     {
    1381           7 :       for (i=1; i < lg(prime); i++) gunclone(gel(prime,i));
    1382           7 :       setlg(prime, 1);
    1383             :     }
    1384             :     else
    1385             :     {
    1386           7 :       for (i=1; i < lg(prime); i++) rmprime(T, gel(prime,i));
    1387             :     }
    1388          14 :     return T;
    1389             :   }
    1390          14 :   rmprime(T, prime); return T;
    1391             : }
    1392             : GEN
    1393          28 : removeprimes(GEN prime) { return rmprimes(primetab, prime); }

Generated by: LCOV version 1.11