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.8.0 lcov report (development 19369-efd6c3d) Lines: 561 622 90.2 %
Date: 2016-08-29 06:11:50 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       79008 : init_MR_Jaeschke(MR_Jaeschke_t *S, GEN n)
      33             : {
      34       79008 :   if (signe(n) < 0) n = absi(n);
      35       79008 :   S->n = n;
      36       79008 :   S->t = addsi(-1,n);
      37       79008 :   S->r1 = vali(S->t);
      38       79008 :   S->t1 = shifti(S->t, -S->r1);
      39       79008 :   S->sqrt1 = cgeti(lg(n)); S->sqrt1[1] = evalsigne(0)|evallgefint(2);
      40       79008 :   S->sqrt2 = cgeti(lg(n)); S->sqrt2[1] = evalsigne(0)|evallgefint(2);
      41       79008 : }
      42             : static void
      43    43815649 : Fl_init_MR_Jaeschke(Fl_MR_Jaeschke_t *S, ulong n)
      44             : {
      45    43815649 :   S->n = n;
      46    43815649 :   S->t = n-1;
      47    43815649 :   S->r1 = vals(S->t);
      48    43821604 :   S->t1 = S->t >> S->r1;
      49    43821604 :   S->sqrt1 = 0;
      50    43821604 :   S->sqrt2 = 0;
      51    43821604 : }
      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        6978 : MR_Jaeschke_ok(MR_Jaeschke_t *S, GEN c)
      59             : {
      60        6978 :   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        6919 :     affii(c, S->sqrt1);
      72        6919 :     affii(subii(S->n, c), S->sqrt2);
      73             :   }
      74        6978 :   return 0;
      75             : }
      76             : static int
      77     4950309 : Fl_MR_Jaeschke_ok(Fl_MR_Jaeschke_t *S, ulong c)
      78             : {
      79     4950309 :   if (S->sqrt1)
      80             :   { /* saw one earlier: compare */
      81         504 :     if (c != S->sqrt1 && c != S->sqrt2) return 1;
      82             :   } else { /* remember */
      83     4949805 :     S->sqrt1 = c;
      84     4949805 :     S->sqrt2 = S->n - c;
      85             :   }
      86     4950309 :   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       79136 : bad_for_base(MR_Jaeschke_t *S, GEN a)
      93             : {
      94       79136 :   pari_sp av = avma;
      95             :   long r;
      96       79136 :   GEN c2, c = Fp_pow(a, S->t1, S->n);
      97             : 
      98       79135 :   if (is_pm1(c) || equalii(S->t, c)) return 0;
      99             : 
     100             :   /* go fishing for -1, not for 1 (saves one squaring) */
     101      137327 :   for (r = S->r1 - 1; r; r--) /* r1 - 1 squarings */
     102             :   {
     103       73827 :     c2 = c; c = remii(sqri(c), S->n);
     104       73827 :     if (equalii(S->t, c)) return MR_Jaeschke_ok(S, c2);
     105       66849 :     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       63500 :   return 1;
     112             : }
     113             : static int
     114    43822730 : Fl_bad_for_base(Fl_MR_Jaeschke_t *S, ulong a)
     115             : {
     116             :   long r;
     117    43822730 :   ulong c2, c = Fl_powu(a, S->t1, S->n);
     118             : 
     119    43829917 :   if (c == 1 || c == S->t) return 0;
     120             : 
     121             :   /* go fishing for -1, not for 1 (saves one squaring) */
     122    64378042 :   for (r = S->r1 - 1; r; r--) /* r1 - 1 squarings */
     123             :   {
     124    30226264 :     c2 = c; c = Fl_sqr(c, S->n);
     125    30226526 :     if (c == S->t) return Fl_MR_Jaeschke_ok(S, c2);
     126             :   }
     127    34151778 :   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        1710 : Fl_MR_Jaeschke(ulong n, long k)
     195             : {
     196        1710 :   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        1710 :   if (!(n & 1)) return 0;
     204        1710 :   if (k == 16)
     205             :   { /* use smaller (faster) bases if possible */
     206           0 :     p = (n < 3215031751UL)? pr: pr+13;
     207           0 :     k = 4;
     208             :   }
     209        1710 :   else if (k == 17)
     210             :   {
     211        1710 :     p = (n < 1373653UL)? pr: pr+11;
     212        1710 :     k = 2;
     213             :   }
     214           0 :   else p = pr; /* 2,3,5,... */
     215        1710 :   Fl_init_MR_Jaeschke(&S, n);
     216        5046 :   for (i=1; i<=k; i++)
     217             :   {
     218        3378 :     r = p[i] % n; if (!r) break;
     219        3378 :     if (Fl_bad_for_base(&S, r)) return 0;
     220             :   }
     221        1668 :   return 1;
     222             : }
     223             : 
     224             : int
     225        1845 : MR_Jaeschke(GEN n, long k)
     226             : {
     227        1845 :   pari_sp av2, av = avma;
     228        1845 :   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        1845 :   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       15380 : LucasMod(GEN n, ulong P, GEN N)
     259             : {
     260       15380 :   pari_sp av = avma;
     261       15380 :   GEN nd = int_MSW(n);
     262       15380 :   ulong m = *nd;
     263             :   long i, j;
     264       15380 :   GEN v = utoipos(P), v1 = utoipos(P*P - 2);
     265             : 
     266       15380 :   if (m == 1)
     267        1128 :     j = 0;
     268             :   else
     269             :   {
     270       14252 :     j = 1+bfffo(m); /* < BIL */
     271       14252 :     m <<= j; j = BITS_IN_LONG - j;
     272             :   }
     273       15380 :   for (i=lgefint(n)-2;;) /* cf. leftright_pow */
     274             :   {
     275     1017748 :     for (; j; m<<=1,j--)
     276             :     { /* v = v_k, v1 = v_{k+1} */
     277      990122 :       if (m&HIGHBIT)
     278             :       { /* set v = v_{2k+1}, v1 = v_{2k+2} */
     279      157697 :         v = subiu(mulii(v,v1), P);
     280      157697 :         v1= subiu(sqri(v1), 2);
     281             :       }
     282             :       else
     283             :       {/* set v = v_{2k}, v1 = v_{2k+1} */
     284      832425 :         v1= subiu(mulii(v,v1), P);
     285      832425 :         v = subiu(sqri(v), 2);
     286             :       }
     287      990122 :       v = modii(v, N);
     288      990122 :       v1= modii(v1,N);
     289      990122 :       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       43006 :     if (--i == 0) return v;
     296       12246 :     j = BITS_IN_LONG;
     297       12246 :     nd=int_precW(nd);
     298       12246 :     m = *nd;
     299       12246 :   }
     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     7784047 : u_LucasMod(ulong n, ulong P, ulong N)
     306             : {
     307             :   ulong v, v1, mP, m2, m;
     308             :   long j;
     309             : 
     310     7784047 :   if (n == 1) return P;
     311     7784028 :   j = 1 + bfffo(n); /* < BIL */
     312     7784028 :   v = P; v1 = P*P - 2; mP = N - P; m2 = N - 2;
     313     7784028 :   m = n<<j; j = BITS_IN_LONG - j;
     314   476715410 :   for (; j; m<<=1,j--)
     315             :   { /* v = v_k, v1 = v_{k+1} */
     316   468931382 :     if (m & HIGHBIT)
     317             :     { /* set v = v_{2k+1}, v1 = v_{2k+2} */
     318    28660945 :       v = Fl_add(Fl_mul(v,v1,N), mP, N);
     319    28660945 :       v1= Fl_add(Fl_mul(v1,v1,N),m2, N);
     320             :     }
     321             :     else
     322             :     {/* set v = v_{2k}, v1 = v_{2k+1} */
     323   440270437 :       v1= Fl_add(Fl_mul(v,v1,N),mP, N);
     324   440270437 :       v = Fl_add(Fl_mul(v,v,N), m2, N);
     325             :     }
     326             :   }
     327     7784028 :   return v;
     328             : }
     329             : 
     330             : int
     331     7784054 : uislucaspsp(ulong n)
     332             : {
     333             :   long i, v;
     334     7784054 :   ulong b, z, m2, m = n + 1;
     335             : 
     336     8563142 :   for (b=3, i=0;; b+=2, i++)
     337             :   {
     338     8563142 :     ulong c = b*b - 4; /* = 1 mod 4 */
     339     8563142 :     if (krouu(n % c, c) < 0) break;
     340      779095 :     if (i == 64 && uissquareall(n, &c)) return 0; /* oo loop if N = m^2 */
     341      779088 :   }
     342     7784047 :   if (!m) return 0; /* neither 2^32-1 nor 2^64-1 are Lucas-pp */
     343     7784047 :   v = vals(m); m >>= v;
     344     7784047 :   z = u_LucasMod(m, b, n);
     345     7784047 :   if (z == 2) return 1;
     346     7729450 :   m2 = n - 2;
     347     7729450 :   if (z == m2) return 1;
     348     3346073 :   for (i=1; i<v; i++)
     349             :   {
     350     3346029 :     if (!z) return 1;
     351      283703 :     z = Fl_add(Fl_mul(z,z, n), m2, n);
     352      283703 :     if (z == 2) return 0;
     353             :   }
     354          44 :   return 0;
     355             : }
     356             : /* N > 3. Caller should check that N is not a square first (taken care of here,
     357             :  * but inefficient) */
     358             : static int
     359       15380 : IsLucasPsP(GEN N)
     360             : {
     361       15380 :   pari_sp av = avma;
     362             :   GEN N_2, m, z;
     363             :   long i, v;
     364             :   ulong b;
     365             : 
     366       34725 :   for (b=3;; b+=2)
     367             :   {
     368       34725 :     ulong c = b*b - 4; /* = 1 mod 4 */
     369       34725 :     if (b == 129 && Z_issquare(N)) return 0; /* avoid oo loop if N = m^2 */
     370       34725 :     if (krouu(umodiu(N,c), c) < 0) break;
     371       19345 :   }
     372       15380 :   m = addis(N,1); v = vali(m); m = shifti(m,-v);
     373       15380 :   z = LucasMod(m, b, N);
     374       15380 :   if (absequaliu(z, 2)) return 1;
     375       13439 :   N_2 = subis(N,2);
     376       13439 :   if (equalii(z, N_2)) return 1;
     377       14496 :   for (i=1; i<v; i++)
     378             :   {
     379       14393 :     if (!signe(z)) return 1;
     380        8005 :     z = modii(subis(sqri(z), 2), N);
     381        8005 :     if (absequaliu(z, 2)) return 0;
     382        8005 :     if (gc_needed(av,1))
     383             :     {
     384           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"IsLucasPsP");
     385           0 :       z = gerepileupto(av, z);
     386             :     }
     387             :   }
     388         103 :   return 0;
     389             : }
     390             : 
     391             : /* assume u odd, u > 1 */
     392             : static int
     393      269691 : iu_coprime(GEN N, ulong u)
     394             : {
     395      269691 :   const ulong n = umodiu(N, u);
     396      269697 :   return (n == 1 || gcduodd(n, u) == 1);
     397             : }
     398             : /* assume u odd, u > 1 */
     399             : static int
     400   143067386 : uu_coprime(ulong n, ulong u)
     401             : {
     402   143067386 :   return gcduodd(n, u) == 1;
     403             : }
     404             : 
     405             : /* composite strong 2-pseudoprime < 1016801 whose prime divisors are > 101 */
     406             : static int
     407     2193772 : is_2_prp_101(ulong n)
     408             : {
     409     2193772 :   switch(n) {
     410             :   case 42799:
     411             :   case 49141:
     412             :   case 88357:
     413             :   case 90751:
     414             :   case 104653:
     415             :   case 130561:
     416             :   case 196093:
     417             :   case 220729:
     418             :   case 253241:
     419             :   case 256999:
     420             :   case 271951:
     421             :   case 280601:
     422             :   case 357761:
     423             :   case 390937:
     424             :   case 458989:
     425             :   case 486737:
     426             :   case 489997:
     427             :   case 514447:
     428             :   case 580337:
     429             :   case 741751:
     430             :   case 838861:
     431             :   case 873181:
     432             :   case 877099:
     433             :   case 916327:
     434             :   case 976873:
     435         210 :   case 983401: return 1;
     436     2193562 :   } return 0;
     437             : }
     438             : 
     439             : static int
     440    43811984 : u_2_prp(ulong n)
     441             : {
     442             :   Fl_MR_Jaeschke_t S;
     443    43811984 :   Fl_init_MR_Jaeschke(&S, n);
     444    43821107 :   return Fl_bad_for_base(&S, 2) == 0;
     445             : }
     446             : static int
     447    41615518 : uBPSW_psp(ulong n) { return (u_2_prp(n) && uislucaspsp(n)); }
     448             : 
     449             : int
     450    97101984 : uisprime(ulong n)
     451             : {
     452    97101984 :   if (n < 103)
     453      862862 :     switch(n)
     454             :     {
     455             :       case 2:
     456             :       case 3:
     457             :       case 5:
     458             :       case 7:
     459             :       case 11:
     460             :       case 13:
     461             :       case 17:
     462             :       case 19:
     463             :       case 23:
     464             :       case 29:
     465             :       case 31:
     466             :       case 37:
     467             :       case 41:
     468             :       case 43:
     469             :       case 47:
     470             :       case 53:
     471             :       case 59:
     472             :       case 61:
     473             :       case 67:
     474             :       case 71:
     475             :       case 73:
     476             :       case 79:
     477             :       case 83:
     478             :       case 89:
     479             :       case 97:
     480      655339 :       case 101: return 1;
     481      207523 :       default: return 0;
     482             :     }
     483    96239122 :   if (!odd(n)) return 0;
     484             : #ifdef LONG_IS_64BIT
     485             :   /* 16294579238595022365 = 3*5*7*11*13*17*19*23*29*31*37*41*43*47*53
     486             :    *  7145393598349078859 = 59*61*67*71*73*79*83*89*97*101 */
     487   139150773 :   if (!uu_coprime(n, 16294579238595022365UL) ||
     488    95126559 :       !uu_coprime(n,  7145393598349078859UL)) return 0;
     489             : #else
     490             :   /* 4127218095 = 3*5*7*11*13*17*19*23*37
     491             :    * 3948078067 = 29*31*41*43*47*53
     492             :    * 4269855901 = 59*83*89*97*101
     493             :    * 1673450759 = 61*67*71*73*79 */
     494     2460663 :   if (!uu_coprime(n, 4127218095UL) ||
     495     1755625 :       !uu_coprime(n, 3948078067UL) ||
     496     1591645 :       !uu_coprime(n, 1673450759UL) ||
     497     1561940 :       !uu_coprime(n, 4269855901UL)) return 0;
     498             : #endif
     499    44819745 :   if (n < 10427) return 1;
     500    43765564 :   if (n < 1016801) return !is_2_prp_101(n) && u_2_prp(n);
     501    41576990 :   return uBPSW_psp(n);
     502             : }
     503             : 
     504             : /* assume no prime divisor <= 101 */
     505             : int
     506       16481 : uisprime_101(ulong n)
     507             : {
     508       16481 :   if (n < 10427) return 1;
     509       16474 :   if (n < 1016801) return !is_2_prp_101(n) && u_2_prp(n);
     510       12708 :   return uBPSW_psp(n);
     511             : }
     512             : 
     513             : /* assume no prime divisor <= 661 */
     514             : int
     515       25820 : uisprime_661(ulong n) { return uBPSW_psp(n); }
     516             : 
     517             : long
     518     4710382 : BPSW_psp(GEN N)
     519             : {
     520             :   pari_sp av;
     521             :   MR_Jaeschke_t S;
     522             :   int k;
     523             : 
     524     4710382 :   if (typ(N) != t_INT) pari_err_TYPE("BPSW_psp",N);
     525     4844197 :   if (signe(N) <= 0) return 0;
     526     4851639 :   if (lgefint(N) == 3) return uisprime(uel(N,2));
     527      115955 :   if (!mod2(N)) return 0;
     528             : #ifdef LONG_IS_64BIT
     529             :   /* 16294579238595022365 = 3*5*7*11*13*17*19*23*29*31*37*41*43*47*53
     530             :    *  7145393598349078859 = 59*61*67*71*73*79*83*89*97*101 */
     531       98663 :   if (!iu_coprime(N, 16294579238595022365UL) ||
     532       57286 :       !iu_coprime(N,  7145393598349078859UL)) return 0;
     533             : #else
     534             :   /* 4127218095 = 3*5*7*11*13*17*19*23*37
     535             :    * 3948078067 = 29*31*41*43*47*53
     536             :    * 4269855901 = 59*83*89*97*101
     537             :    * 1673450759 = 61*67*71*73*79 */
     538       99260 :   if (!iu_coprime(N, 4127218095UL) ||
     539       78950 :       !iu_coprime(N, 3948078067UL) ||
     540       71772 :       !iu_coprime(N, 1673450759UL) ||
     541       58886 :       !iu_coprime(N, 4269855901UL)) return 0;
     542             : #endif
     543             :   /* no prime divisor < 103 */
     544       74576 :   av = avma;
     545       74576 :   init_MR_Jaeschke(&S, N);
     546       74576 :   k = (!bad_for_base(&S, gen_2) && IsLucasPsP(N));
     547       74576 :   avma = av; return k;
     548             : }
     549             : 
     550             : /* can we write n = x^k ? Assume N has no prime divisor <= 2^14.
     551             :  * Not memory clean */
     552             : long
     553        7682 : isanypower_nosmalldiv(GEN N, GEN *px)
     554             : {
     555        7682 :   GEN x = N, y;
     556        7682 :   ulong mask = 7;
     557        7682 :   long ex, k = 1;
     558             :   forprime_t T;
     559        7682 :   while (Z_issquareall(x, &y)) { k <<= 1; x = y; }
     560        7682 :   while ( (ex = is_357_power(x, &y, &mask)) ) { k *= ex; x = y; }
     561        7682 :   (void)u_forprime_init(&T, 11, ULONG_MAX);
     562             :   /* stop when x^(1/k) < 2^14 */
     563        7682 :   while ( (ex = is_pth_power(x, &y, &T, 15)) ) { k *= ex; x = y; }
     564        7682 :   *px = x; return k;
     565             : }
     566             : 
     567             : /* no prime divisor <= 2^14 (> 661) */
     568             : long
     569       14342 : BPSW_psp_nosmalldiv(GEN N)
     570             : {
     571             :   pari_sp av;
     572             :   MR_Jaeschke_t S;
     573       14342 :   long l = lgefint(N);
     574             :   int k;
     575             : 
     576       14342 :   if (l == 3) return uisprime_661(uel(N,2));
     577        4311 :   av = avma;
     578             :   /* N large: test for pure power, rarely succeeds, but requires < 1% of
     579             :    * compositeness test times */
     580        4311 :   if (bit_accuracy(l) > 512 && isanypower_nosmalldiv(N, &N) != 1)
     581             :   {
     582          21 :     avma = av; return 0;
     583             :   }
     584        4290 :   init_MR_Jaeschke(&S, N);
     585        4290 :   k = (!bad_for_base(&S, gen_2) && IsLucasPsP(N));
     586        4290 :   avma = av; return k;
     587             : }
     588             : 
     589             : /***********************************************************************/
     590             : /**                                                                   **/
     591             : /**                       Pocklington-Lehmer                          **/
     592             : /**                        P-1 primality test                         **/
     593             : /**                                                                   **/
     594             : /***********************************************************************/
     595             : /* Assume x BPSW pseudoprime. Check whether it's small enough to be certified
     596             :  * prime (< 2^64). Reference for strong 2-pseudoprimes:
     597             :  *   http://www.cecm.sfu.ca/Pseudoprimes/index-2-to-64.html */
     598             : static int
     599      949023 : BPSW_isprime_small(GEN x)
     600             : {
     601      949023 :   long l = lgefint(x);
     602             : #ifdef LONG_IS_64BIT
     603      883806 :   return (l == 3);
     604             : #else
     605       65217 :   return (l <= 4);
     606             : #endif
     607             : }
     608             : 
     609             : /* Assume N > 1, p^e || N-1, p prime. Find a witness a(p) such that
     610             :  *   a^(N-1) = 1 (mod N)
     611             :  *   a^(N-1)/p - 1 invertible mod N.
     612             :  * Proves that any divisor of N is 1 mod p^e. Return 0 if N is composite */
     613             : static ulong
     614       15231 : pl831(GEN N, GEN p)
     615             : {
     616       15231 :   GEN b, c, g, Nmunp = diviiexact(addis(N,-1), p);
     617       15231 :   pari_sp av = avma;
     618             :   ulong a;
     619       22328 :   for(a = 2;; a++, avma = av)
     620             :   {
     621       22328 :     b = Fp_pow(utoipos(a), Nmunp, N);
     622       22328 :     if (!equali1(b)) break;
     623        7097 :   }
     624       15231 :   c = Fp_pow(b,p,N);
     625       15231 :   g = gcdii(addis(b,-1), N); /* 0 < g < N */
     626       15231 :   return (equali1(c) && equali1(g))? a: 0;
     627             : }
     628             : 
     629             : /* Brillhart, Lehmer, Selfridge test (Crandall & Pomerance, Th 4.1.5)
     630             :  * N^(1/3) <= f fully factored, f | N-1. If pl831(p) is true for
     631             :  * any prime divisor p of f, then any divisor of N is 1 mod f.
     632             :  * In that case return 1 iff N is prime */
     633             : static int
     634          63 : BLS_test(GEN N, GEN f)
     635             : {
     636             :   GEN c1, c2, r, q;
     637          63 :   q = dvmdii(N, f, &r);
     638          63 :   if (!is_pm1(r)) return 0;
     639          63 :   c2 = dvmdii(q, f, &c1);
     640             :   /* N = 1 + f c1 + f^2 c2, 0 <= c_i < f; check whether it is of the form
     641             :    * (1 + fa)(1 + fb) */
     642          63 :   return ! Z_issquare(subii(sqri(c1), shifti(c2,2)));
     643             : }
     644             : 
     645             : /* BPSW_psp(N) && !BPSW_isprime_small(N). Decide between Pocklington-Lehmer
     646             :  * and APRCL. Return a vector of (small) primes such that PL-witnesses
     647             :  * guarantee the primality of N. Return NULL if PL is likely too expensive.
     648             :  * Return gen_0 if BLS test finds N to be composite */
     649             : static GEN
     650        5008 : BPSW_try_PL(GEN N)
     651             : {
     652        5008 :   ulong B = minuu(1UL<<19, maxprime());
     653        5008 :   GEN E, p, U, F, N_1 = subiu(N,1);
     654        5008 :   GEN fa = Z_factor_limit(N_1, B), P = gel(fa,1);
     655        5008 :   long n = lg(P)-1;
     656             : 
     657        5008 :   p = gel(P,n);
     658             :   /* if p prime, then N-1 is fully factored */
     659        5008 :   if (cmpii(p,sqru(B)) <= 0 || (BPSW_psp_nosmalldiv(p) && BPSW_isprime(p)))
     660        3012 :     return P;
     661             : 
     662        1996 :   E = gel(fa,2);
     663        1996 :   U = powii(p, gel(E,n)); /* unfactored part of N-1 */
     664             :   /* factored part of N-1; n >= 2 since 2p | N-1 */
     665        1996 :   F = (n == 2)? powii(gel(P,1), gel(E,1)): diviiexact(N_1,  U);
     666        1996 :   setlg(P, n); /* remove last (composite) entry */
     667             : 
     668             :   /* N-1 = F U, F factored, U possibly composite, (U,F) = 1 */
     669        1996 :   if (cmpii(F, U) > 0) return P; /* 1/2-smooth */
     670        1989 :   if (cmpii(sqri(F), U) > 0) return BLS_test(N,F)? P: gen_0; /* 1/3-smooth */
     671        1933 :   return NULL; /* not smooth enough */
     672             : }
     673             : 
     674             : static GEN isprimePL(GEN N);
     675             : static GEN PL_certificate(GEN N, GEN F);
     676             : /* Assume N a BPSW pseudoprime. Return 0 if not prime, and a primality label
     677             :  * otherwise: 1 (small), 2 (APRCL), or PL-certificate  */
     678             : static GEN
     679          49 : check_prime(GEN N)
     680             : {
     681             :   GEN P;
     682          49 :   if (BPSW_isprime_small(N)) return gen_1;
     683             :   /* PL for small N: APRCL is faster but we prefer a certificate */
     684           0 :   if (expi(N) <= 250) return isprimePL(N);
     685           0 :   P = BPSW_try_PL(N);
     686             :   /* if PL likely too expensive: give up certificate and use APRCL */
     687           0 :   if (!P) return isprimeAPRCL(N)? gen_2: gen_0;
     688           0 :   return typ(P) != t_INT? PL_certificate(N,P): gen_0;
     689             : }
     690             : 
     691             : /* F is a vector whose entries are primes. For each of them, find a PL
     692             :  * witness. Return 0 if caller lied and F contains a composite */
     693             : static long
     694        3075 : PL_certify(GEN N, GEN F)
     695             : {
     696        3075 :   long i, l = lg(F);
     697       18250 :   for(i = 1; i < l; i++)
     698       15175 :     if (! pl831(N, gel(F,i))) return 0;
     699        3075 :   return 1;
     700             : }
     701             : /* F is a vector whose entries are *believed* to be primes. For each of them,
     702             :  * recording a witness and recursive primality certificate */
     703             : static GEN
     704          28 : PL_certificate(GEN N, GEN F)
     705             : {
     706          28 :   long i, l = lg(F);
     707          28 :   GEN W = cgetg(l,t_COL);
     708          28 :   GEN R = cgetg(l,t_COL);
     709          77 :   for(i=1; i<l; i++)
     710             :   {
     711          56 :     GEN p = gel(F,i);
     712          56 :     ulong witness = pl831(N,p);
     713          56 :     if (!witness) return gen_0;
     714          49 :     gel(W,i) = utoipos(witness);
     715          49 :     gel(R,i) = check_prime(p);
     716          49 :     if (isintzero(gel(R,i)))
     717             :     { /* composite in prime factorisation ! */
     718           0 :       err_printf("Not a prime: %Ps", p);
     719           0 :       pari_err_BUG("PL_certificate [false prime number]");
     720             :     }
     721             :   }
     722          21 :   return mkmat3(F, W, R);
     723             : }
     724             : /* Assume N is a strong BPSW pseudoprime, Pocklington-Lehmer primality proof.
     725             :  * Return gen_0 (non-prime), gen_1 (small prime), matrix (large prime)
     726             :  *
     727             :  * The matrix has 3 columns, [a,b,c] with
     728             :  * a[i] prime factor of N-1,
     729             :  * b[i] witness for a[i] as in pl831
     730             :  * c[i] check_prime(a[i]) */
     731             : static GEN
     732          35 : isprimePL(GEN N)
     733             : {
     734          35 :   pari_sp ltop = avma;
     735             :   GEN cbrtN, N_1, F, f;
     736             : 
     737          35 :   if (typ(N) != t_INT) pari_err_TYPE("isprimePL",N);
     738          35 :   switch(cmpis(N,2))
     739             :   {
     740           0 :     case -1:return gen_0;
     741           7 :     case 0: return gen_1;
     742             :   }
     743             :   /* N > 2 */
     744          28 :   cbrtN = sqrtnint(N, 3);
     745          28 :   N_1 = addis(N,-1);
     746          28 :   F = Z_factor_until(N_1, sqri(cbrtN));
     747          28 :   f = factorback(F); /* factored part of N-1, f^3 > N */
     748          28 :   if (DEBUGLEVEL>3)
     749             :   {
     750           0 :     GEN r = divri(itor(f,LOWDEFAULTPREC), N);
     751           0 :     err_printf("Pocklington-Lehmer: proving primality of N = %Ps\n", N);
     752           0 :     err_printf("Pocklington-Lehmer: N-1 factored up to %Ps! (%.3Ps%%)\n", f, r);
     753             :   }
     754             :   /* if N-1 is only N^(1/3)-smooth, BLS test */
     755          28 :   if (!equalii(f,N_1) && cmpii(sqri(f),N) <= 0 && !BLS_test(N,f))
     756           0 :   { avma = ltop; return gen_0; } /* Failed, N is composite */
     757          28 :   return gerepilecopy(ltop, PL_certificate(N, gel(F,1)));
     758             : }
     759             : 
     760             : /* assume N a BPSW pseudoprime, in particular, it is odd > 2. Prove N prime */
     761             : long
     762      948558 : BPSW_isprime(GEN N)
     763             : {
     764             :   pari_sp av;
     765             :   long t;
     766             :   GEN P;
     767      948558 :   if (BPSW_isprime_small(N)) return 1;
     768        5008 :   av = avma; P = BPSW_try_PL(N);
     769        5008 :   if (!P)
     770        1933 :     t = isprimeAPRCL(N); /* not smooth enough */
     771             :   else
     772        3075 :     t = (typ(P) == t_INT)? 0: PL_certify(N,P);
     773        5008 :   avma = av; return t;
     774             : }
     775             : 
     776             : GEN
     777     3725725 : gisprime(GEN x, long flag)
     778             : {
     779     3725725 :   switch (flag)
     780             :   {
     781     3725690 :     case 0: return map_proto_lG(isprime,x);
     782          21 :     case 1: return map_proto_G(isprimePL,x);
     783          14 :     case 2: return map_proto_lG(isprimeAPRCL,x);
     784             :   }
     785           0 :   pari_err_FLAG("gisprime");
     786           0 :   return NULL;
     787             : }
     788             : 
     789             : long
     790     4446439 : isprime(GEN x) { return BPSW_psp(x) && BPSW_isprime(x); }
     791             : 
     792             : /***********************************************************************/
     793             : /**                                                                   **/
     794             : /**                          PRIME NUMBERS                            **/
     795             : /**                                                                   **/
     796             : /***********************************************************************/
     797             : 
     798             : static struct {
     799             :   ulong p;
     800             :   long n;
     801             : } prime_table[] = {
     802             :   {           0,          0},
     803             :   {        7919,       1000},
     804             :   {       17389,       2000},
     805             :   {       27449,       3000},
     806             :   {       37813,       4000},
     807             :   {       48611,       5000},
     808             :   {       59359,       6000},
     809             :   {       70657,       7000},
     810             :   {       81799,       8000},
     811             :   {       93179,       9000},
     812             :   {      104729,      10000},
     813             :   {      224737,      20000},
     814             :   {      350377,      30000},
     815             :   {      479909,      40000},
     816             :   {      611953,      50000},
     817             :   {      746773,      60000},
     818             :   {      882377,      70000},
     819             :   {     1020379,      80000},
     820             :   {     1159523,      90000},
     821             :   {     1299709,     100000},
     822             :   {     2750159,     200000},
     823             :   {     7368787,     500000},
     824             :   {    15485863,    1000000},
     825             :   {    32452843,    2000000},
     826             :   {    86028121,    5000000},
     827             :   {   179424673,   10000000},
     828             :   {   373587883,   20000000},
     829             :   {   982451653,   50000000},
     830             :   {  2038074743,  100000000},
     831             :   {  4000000483UL,189961831},
     832             :   {  4222234741UL,200000000},
     833             : #if BITS_IN_LONG == 64
     834             :   { 11037271757UL,  500000000L},
     835             :   { 22801763489UL, 1000000000L},
     836             :   { 47055833459UL, 2000000000L},
     837             :   {122430513841UL, 5000000000L},
     838             :   {200000000507UL, 8007105083L},
     839             : #endif
     840             : };
     841             : static const int prime_table_len = numberof(prime_table);
     842             : 
     843             : /* find prime closest to n in prime_table. */
     844             : static long
     845    12479410 : prime_table_closest_p(ulong n)
     846             : {
     847             :   long i;
     848    12807400 :   for (i = 1; i < prime_table_len; i++)
     849             :   {
     850    12807335 :     ulong p = prime_table[i].p;
     851    12807335 :     if (p > n)
     852             :     {
     853    12479345 :       ulong u = n - prime_table[i-1].p;
     854    12479345 :       if (p - n > u) i--;
     855    12479345 :       break;
     856             :     }
     857             :   }
     858    12479410 :   if (i == prime_table_len) i = prime_table_len - 1;
     859    12479410 :   return i;
     860             : }
     861             : 
     862             : /* return the n-th successor of prime p > 2 */
     863             : static GEN
     864          70 : prime_successor(ulong p, ulong n)
     865             : {
     866             :   forprime_t S;
     867             :   ulong i;
     868          70 :   forprime_init(&S, utoipos(p+1), NULL);
     869          70 :   for (i = 1; i < n; i++) (void)forprime_next(&S);
     870          70 :   return forprime_next(&S);
     871             : }
     872             : /* find the N-th prime */
     873             : static GEN
     874         161 : prime_table_find_n(ulong N)
     875             : {
     876             :   byteptr d;
     877         161 :   ulong n, p, maxp = maxprime();
     878             :   long i;
     879        2037 :   for (i = 1; i < prime_table_len; i++)
     880             :   {
     881        2037 :     n = prime_table[i].n;
     882        2037 :     if (n > N)
     883             :     {
     884         161 :       ulong u = N - prime_table[i-1].n;
     885         161 :       if (n - N > u) i--;
     886         161 :       break;
     887             :     }
     888             :   }
     889         161 :   if (i == prime_table_len) i = prime_table_len - 1;
     890         161 :   p = prime_table[i].p;
     891         161 :   n = prime_table[i].n;
     892         161 :   if (n > N && p > maxp)
     893             :   {
     894          14 :     i--;
     895          14 :     p = prime_table[i].p;
     896          14 :     n = prime_table[i].n;
     897             :   }
     898             :   /* if beyond prime table, then n <= N */
     899         161 :   d = diffptr + n;
     900         161 :   if (n > N)
     901             :   {
     902          14 :     n -= N;
     903       50624 :     do { n--; PREC_PRIME_VIADIFF(p,d); } while (n) ;
     904             :   }
     905         147 :   else if (n < N)
     906             :   {
     907         147 :     n = N-n;
     908         147 :     if (p > maxp) return prime_successor(p, n);
     909             :     do {
     910       44492 :       if (!*d) return prime_successor(p, n);
     911       44492 :       n--; NEXT_PRIME_VIADIFF(p,d);
     912       44492 :     } while (n) ;
     913             :   }
     914          91 :   return utoipos(p);
     915             : }
     916             : 
     917             : ulong
     918           0 : uprime(long N)
     919             : {
     920           0 :   pari_sp av = avma;
     921             :   GEN p;
     922           0 :   if (N <= 0) pari_err_DOMAIN("prime", "n", "<=",gen_0, stoi(N));
     923           0 :   p = prime_table_find_n(N);
     924           0 :   if (lgefint(p) != 3) pari_err_OVERFLOW("uprime");
     925           0 :   avma = av; return p[2];
     926             : }
     927             : GEN
     928         168 : prime(long N)
     929             : {
     930         168 :   pari_sp av = avma;
     931             :   GEN p;
     932         168 :   if (N <= 0) pari_err_DOMAIN("prime", "n", "<=",gen_0, stoi(N));
     933         161 :   new_chunk(4); /*HACK*/
     934         161 :   p = prime_table_find_n(N);
     935         161 :   avma = av; return icopy(p);
     936             : }
     937             : 
     938             : /* random b-bit prime */
     939             : GEN
     940          49 : randomprime(GEN N)
     941             : {
     942          49 :   pari_sp av = avma, av2;
     943             :   GEN a, b, d;
     944          49 :   if (!N)
     945             :     for(;;)
     946             :     {
     947          63 :       ulong p = random_bits(31);
     948          63 :       if (uisprime(p)) return utoipos(p);
     949          56 :     }
     950          42 :   switch(typ(N))
     951             :   {
     952             :     case t_INT:
     953          14 :       a = gen_2;
     954          14 :       b = subiu(N,1); /* between 2 and N-1 */
     955          14 :       d = subiu(N,2);
     956          14 :       if (signe(d) <= 0)
     957           7 :         pari_err_DOMAIN("randomprime","N", "<=", gen_2, N);
     958           7 :       break;
     959             :     case t_VEC:
     960          28 :       if (lg(N) != 3) pari_err_TYPE("randomprime",N);
     961          28 :       a = gel(N,1);
     962          28 :       b = gel(N,2);
     963          28 :       if (gcmp(b, a) < 0)
     964           7 :         pari_err_DOMAIN("randomprime","b-a", "<", gen_0, mkvec2(a,b));
     965          21 :       if (typ(a) != t_INT)
     966             :       {
     967           7 :         a = gceil(a);
     968           7 :         if (typ(a) != t_INT) pari_err_TYPE("randomprime",a);
     969             :       }
     970          21 :       if (typ(b) != t_INT)
     971             :       {
     972           7 :         b = gfloor(b);
     973           7 :         if (typ(b) != t_INT) pari_err_TYPE("randomprime",b);
     974             :       }
     975          21 :       if (cmpis(a, 2) < 0)
     976             :       {
     977           7 :         a = gen_2;
     978           7 :         d = subiu(b,1);
     979             :       }
     980             :       else
     981          14 :         d = addiu(subii(b,a), 1);
     982          21 :       if (signe(d) <= 0)
     983          14 :         pari_err_DOMAIN("randomprime","floor(b) - max(ceil(a),2)", "<",
     984             :                         gen_0, mkvec2(a,b));
     985           7 :       break;
     986             :     default:
     987           0 :       pari_err_TYPE("randomprime", N);
     988           0 :       return NULL; /*notreached*/
     989             :   }
     990          14 :   av2 = avma;
     991             :   for (;;)
     992             :   {
     993         210 :     GEN p = addii(a, randomi(d));
     994         210 :     if (BPSW_psp(p)) return gerepileuptoint(av, p);
     995         196 :     avma = av2;
     996         196 :   }
     997             : }
     998             : 
     999             : /* set *pp = nextprime(a) = p
    1000             :  *     *pd so that NEXT_PRIME_VIADIFF(d, p) = nextprime(p+1)
    1001             :  *     *pn so that p = the n-th prime
    1002             :  * error if nextprime(a) is out of primetable bounds */
    1003             : void
    1004    12479257 : prime_table_next_p(ulong a, byteptr *pd, ulong *pp, ulong *pn)
    1005             : {
    1006             :   byteptr d;
    1007    12479257 :   ulong p, n, maxp = maxprime();
    1008    12479316 :   long i = prime_table_closest_p(a);
    1009    12479278 :   p = prime_table[i].p;
    1010    12479278 :   if (p > a && p > maxp)
    1011             :   {
    1012           0 :     i--;
    1013           0 :     p = prime_table[i].p;
    1014             :   }
    1015             :   /* if beyond prime table, then p <= a */
    1016    12479278 :   n = prime_table[i].n;
    1017    12479278 :   d = diffptr + n;
    1018    12479278 :   if (p < a)
    1019             :   {
    1020    12374691 :     if (a > maxp) pari_err_MAXPRIME(a);
    1021    27703551 :     do { n++; NEXT_PRIME_VIADIFF(p,d); } while (p < a);
    1022             :   }
    1023      104587 :   else if (p != a)
    1024             :   {
    1025     2340819 :     do { n--; PREC_PRIME_VIADIFF(p,d); } while (p > a) ;
    1026        8604 :     if (p < a) { NEXT_PRIME_VIADIFF(p,d); n++; }
    1027             :   }
    1028    12479358 :   *pn = n;
    1029    12479358 :   *pp = p;
    1030    12479358 :   *pd = d;
    1031    12479358 : }
    1032             : 
    1033             : ulong
    1034        9267 : uprimepi(ulong a)
    1035             : {
    1036        9267 :   ulong p, n, maxp = maxprime();
    1037        9267 :   if (a <= maxp)
    1038             :   {
    1039             :     byteptr d;
    1040        9149 :     prime_table_next_p(a, &d, &p, &n);
    1041        9149 :     return p == a? n: n-1;
    1042             :   }
    1043             :   else
    1044             :   {
    1045         118 :     long i = prime_table_closest_p(a);
    1046             :     forprime_t S;
    1047         118 :     p = prime_table[i].p;
    1048         118 :     if (p > a)
    1049             :     {
    1050          28 :       i--;
    1051          28 :       p = prime_table[i].p;
    1052             :     }
    1053             :     /* p = largest prime in table <= a */
    1054         118 :     n = prime_table[i].n;
    1055         118 :     (void)u_forprime_init(&S, p+1, a);
    1056         118 :     for (; p; n++) p = u_forprime_next(&S);
    1057         118 :     return n-1;
    1058             :   }
    1059             : }
    1060             : 
    1061             : GEN
    1062         245 : primepi(GEN x)
    1063             : {
    1064         245 :   pari_sp av = avma;
    1065         245 :   GEN pp, nn, N = typ(x) == t_INT? x: gfloor(x);
    1066             :   forprime_t S;
    1067             :   ulong n, p;
    1068             :   long i, l;
    1069         245 :   if (typ(N) != t_INT) pari_err_TYPE("primepi",N);
    1070         245 :   if (signe(N) <= 0) return gen_0;
    1071         245 :   avma = av; l = lgefint(N);
    1072         245 :   if (l == 3) return utoi(uprimepi(N[2]));
    1073           1 :   i = prime_table_len-1;
    1074           1 :   p = prime_table[i].p;
    1075           1 :   n = prime_table[i].n;
    1076           1 :   (void)forprime_init(&S, utoipos(p+1), N);
    1077           1 :   nn = setloop(utoipos(n));
    1078           1 :   pp = gen_0;
    1079           1 :   for (; pp; incloop(nn)) pp = forprime_next(&S);
    1080           1 :   return gerepileuptoint(av, subiu(nn,1));
    1081             : }
    1082             : 
    1083             : /* pi(x) < x/log x * (1 + 1/log x + 2.51/log^2 x)), x>=355991 [ Dusart ]
    1084             :  * pi(x) < x/(log x - 1.1), x >= 60184 [ Dusart ]
    1085             :  * ? \p9
    1086             :  * ? M = 0; for(x = 4, 60184, M = max(M, log(x) - x/primepi(x))); M
    1087             :  * %1 = 1.11196252 */
    1088             : double
    1089       53526 : primepi_upper_bound(double x)
    1090             : {
    1091       53526 :   if (x >= 355991)
    1092             :   {
    1093          14 :     double L = 1/log(x);
    1094          14 :     return x * L * (1 + L + 2.51*L*L);
    1095             :   }
    1096       53512 :   if (x >= 60184) return x / (log(x) - 1.1);
    1097       53512 :   if (x < 5) return 2; /* don't bother */
    1098       50126 :   return x / (log(x) - 1.111963);
    1099             : }
    1100             : /* pi(x) > x/log x (1 + 1/log x), x >= 599 [ Dusart ]
    1101             :  * pi(x) > x / (log x + 2), x >= 55 [ Rosser ] */
    1102             : double
    1103          14 : primepi_lower_bound(double x)
    1104             : {
    1105          14 :   if (x >= 599)
    1106             :   {
    1107          14 :     double L = 1/log(x);
    1108          14 :     return x * L * (1 + L);
    1109             :   }
    1110           0 :   if (x < 55) return 0; /* don't bother */
    1111           0 :   return x / (log(x) + 2.);
    1112             : }
    1113             : GEN
    1114           1 : gprimepi_upper_bound(GEN x)
    1115             : {
    1116           1 :   pari_sp av = avma;
    1117             :   double L;
    1118           1 :   if (typ(x) != t_INT) x = gfloor(x);
    1119           1 :   if (expi(x) <= 1022)
    1120             :   {
    1121           1 :     avma = av;
    1122           1 :     return dbltor(primepi_upper_bound(gtodouble(x)));
    1123             :   }
    1124           0 :   x = itor(x, LOWDEFAULTPREC);
    1125           0 :   L = 1 / rtodbl(logr_abs(x));
    1126           0 :   x = mulrr(x, dbltor(L * (1 + L + 2.51*L*L)));
    1127           0 :   return gerepileuptoleaf(av, x);
    1128             : }
    1129             : GEN
    1130           1 : gprimepi_lower_bound(GEN x)
    1131             : {
    1132           1 :   pari_sp av = avma;
    1133             :   double L;
    1134           1 :   if (typ(x) != t_INT) x = gfloor(x);
    1135           1 :   if (abscmpiu(x, 55) <= 0) return gen_0;
    1136           1 :   if (expi(x) <= 1022)
    1137             :   {
    1138           1 :     avma = av;
    1139           1 :     return dbltor(primepi_lower_bound(gtodouble(x)));
    1140             :   }
    1141           0 :   x = itor(x, LOWDEFAULTPREC);
    1142           0 :   L = 1 / rtodbl(logr_abs(x));
    1143           0 :   x = mulrr(x, dbltor(L * (1 + L)));
    1144           0 :   return gerepileuptoleaf(av, x);
    1145             : }
    1146             : 
    1147             : GEN
    1148          63 : primes(long n)
    1149             : {
    1150             :   forprime_t S;
    1151             :   long i;
    1152             :   GEN y;
    1153          63 :   if (n <= 0) return cgetg(1, t_VEC);
    1154          63 :   y = cgetg(n+1, t_VEC);
    1155          63 :   (void)new_chunk(3*n); /*HACK*/
    1156          63 :   u_forprime_init(&S, 2, ULONG_MAX);
    1157          63 :   avma = (pari_sp)y;
    1158          63 :   for (i = 1; i <= n; i++) gel(y, i) = utoipos( u_forprime_next(&S) );
    1159          63 :   return y;
    1160             : }
    1161             : GEN
    1162           0 : primes_zv(long n)
    1163             : {
    1164             :   forprime_t S;
    1165             :   long i;
    1166             :   GEN y;
    1167           0 :   if (n <= 0) return cgetg(1, t_VECSMALL);
    1168           0 :   y = cgetg(n+1,t_VECSMALL);
    1169           0 :   u_forprime_init(&S, 2, ULONG_MAX);
    1170           0 :   for (i = 1; i <= n; i++) y[i] =  u_forprime_next(&S);
    1171           0 :   avma = (pari_sp)y; return y;
    1172             : }
    1173             : GEN
    1174         119 : primes0(GEN N)
    1175             : {
    1176         119 :   switch(typ(N))
    1177             :   {
    1178          63 :     case t_INT: return primes(itos(N));
    1179             :     case t_VEC:
    1180          56 :       if (lg(N) == 3) return primes_interval(gel(N,1),gel(N,2));
    1181             :   }
    1182           0 :   pari_err_TYPE("primes", N);
    1183           0 :   return NULL;
    1184             : }
    1185             : 
    1186             : GEN
    1187          56 : primes_interval(GEN a, GEN b)
    1188             : {
    1189          56 :   pari_sp av = avma;
    1190             :   forprime_t S;
    1191             :   long i, n;
    1192             :   GEN y, d, p;
    1193          56 :   if (typ(a) != t_INT)
    1194             :   {
    1195           0 :     a = gceil(a);
    1196           0 :     if (typ(a) != t_INT) pari_err_TYPE("primes_interval",a);
    1197             :   }
    1198          56 :   if (typ(b) != t_INT)
    1199             :   {
    1200           7 :     b = gfloor(b);
    1201           7 :     if (typ(b) != t_INT) pari_err_TYPE("primes_interval",b);
    1202             :   }
    1203          49 :   if (signe(a) < 0) a = gen_2;
    1204          49 :   d = subii(b, a);
    1205          49 :   if (signe(d) < 0 || signe(b) <= 0) { avma = av; return cgetg(1, t_VEC); }
    1206          49 :   if (lgefint(b) == 3)
    1207             :   {
    1208          33 :     avma = av;
    1209          33 :     y = primes_interval_zv(itou(a), itou(b));
    1210          33 :     n = lg(y); settyp(y, t_VEC);
    1211          33 :     for (i = 1; i < n; i++) gel(y,i) = utoipos(y[i]);
    1212          33 :     return y;
    1213             :   }
    1214             :   /* at most d+1 primes in [a,b]. If d large, try better bound to lower
    1215             :    * memory use */
    1216          16 :   if (abscmpiu(d,100000) > 0)
    1217             :   {
    1218           1 :     GEN D = gsub(gprimepi_upper_bound(b), gprimepi_lower_bound(a));
    1219           1 :     D = ceil_safe(D);
    1220           1 :     if (cmpii(D, d) < 0) d = D;
    1221             :   }
    1222          16 :   n = itos(d)+1;
    1223          16 :   forprime_init(&S, a, b);
    1224          16 :   y = cgetg(n+1, t_VEC); i = 1;
    1225          16 :   while ((p = forprime_next(&S))) gel(y, i++) = icopy(p);
    1226          16 :   setlg(y, i); return gerepileupto(av, y);
    1227             : }
    1228             : 
    1229             : /* a <= b, at most d primes in [a,b]. Return them */
    1230             : static GEN
    1231         152 : primes_interval_i(ulong a, ulong b, ulong d)
    1232             : {
    1233         152 :   ulong p, i = 1, n = d + 1;
    1234             :   forprime_t S;
    1235         152 :   GEN y = cgetg(n+1, t_VECSMALL);
    1236         152 :   pari_sp av = avma;
    1237         152 :   u_forprime_init(&S, a, b);
    1238         152 :   while ((p = u_forprime_next(&S))) y[i++] = p;
    1239         152 :   avma = av; setlg(y, i); stackdummy((pari_sp)(y + i), (pari_sp)(y + n+1));
    1240         152 :   return y;
    1241             : }
    1242             : GEN
    1243          33 : primes_interval_zv(ulong a, ulong b)
    1244             : {
    1245             :   ulong d;
    1246          33 :   if (!a) return primes_upto_zv(b);
    1247          33 :   if (b < a) return cgetg(1, t_VECSMALL);
    1248          33 :   d = b - a;
    1249          33 :   if (d > 100000UL)
    1250             :   {
    1251          13 :     ulong D = (ulong)ceil(primepi_upper_bound(b)-primepi_lower_bound(a));
    1252          13 :     if (D < d) d = D;
    1253             :   }
    1254          33 :   return primes_interval_i(a, b, d);
    1255             : }
    1256             : GEN
    1257         119 : primes_upto_zv(ulong b)
    1258             : {
    1259             :   ulong d;
    1260         119 :   if (b < 2) return cgetg(1, t_VECSMALL);
    1261         119 :   d = (b > 100000UL)? (ulong)primepi_upper_bound(b): b;
    1262         119 :   return primes_interval_i(2, b, d);
    1263             : }
    1264             : 
    1265             : /***********************************************************************/
    1266             : /**                                                                   **/
    1267             : /**                       PRIVATE PRIME TABLE                         **/
    1268             : /**                                                                   **/
    1269             : /***********************************************************************/
    1270             : /* delete dummy NULL entries */
    1271             : static void
    1272          21 : cleanprimetab(GEN T)
    1273             : {
    1274          21 :   long i,j, l = lg(T);
    1275          70 :   for (i = j = 1; i < l; i++)
    1276          49 :     if (T[i]) T[j++] = T[i];
    1277          21 :   setlg(T,j);
    1278          21 : }
    1279             : /* remove p from T */
    1280             : static void
    1281          28 : rmprime(GEN T, GEN p)
    1282             : {
    1283             :   long i;
    1284          28 :   if (typ(p) != t_INT) pari_err_TYPE("removeprimes",p);
    1285          28 :   i = ZV_search(T, p);
    1286          28 :   if (!i)
    1287           7 :     pari_err_DOMAIN("removeprime","prime","not in",
    1288             :                     strtoGENstr("primetable"), p);
    1289          21 :   gunclone(gel(T,i)); gel(T,i) = NULL;
    1290          21 :   cleanprimetab(T);
    1291          21 : }
    1292             : 
    1293             : /*stolen from ZV_union_shallow() : clone entries from y */
    1294             : static GEN
    1295          28 : addp_union(GEN x, GEN y)
    1296             : {
    1297          28 :   long i, j, k, lx = lg(x), ly = lg(y);
    1298          28 :   GEN z = cgetg(lx + ly - 1, t_VEC);
    1299          28 :   i = j = k = 1;
    1300          63 :   while (i<lx && j<ly)
    1301             :   {
    1302           7 :     int s = cmpii(gel(x,i), gel(y,j));
    1303           7 :     if (s < 0)
    1304           0 :       gel(z,k++) = gel(x,i++);
    1305           7 :     else if (s > 0)
    1306           0 :       gel(z,k++) = gclone(gel(y,j++));
    1307             :     else {
    1308           7 :       gel(z,k++) = gel(x,i++);
    1309           7 :       j++;
    1310             :     }
    1311             :   }
    1312          28 :   while (i<lx) gel(z,k++) = gel(x,i++);
    1313          28 :   while (j<ly) gel(z,k++) = gclone(gel(y,j++));
    1314          28 :   setlg(z, k); return z;
    1315             : }
    1316             : 
    1317             : /* p is NULL, or a single element or a row vector with "primes" to add to
    1318             :  * prime table. */
    1319             : static GEN
    1320         161 : addp(GEN *T, GEN p)
    1321             : {
    1322         161 :   pari_sp av = avma;
    1323             :   long i, l;
    1324             :   GEN v;
    1325             : 
    1326         161 :   if (!p || lg(p) == 1) return *T;
    1327          42 :   if (!is_vec_t(typ(p))) p = mkvec(p);
    1328             : 
    1329          42 :   RgV_check_ZV(p, "addprimes");
    1330          35 :   v = gen_indexsort_uniq(p, (void*)&cmpii, &cmp_nodata);
    1331          35 :   p = vecpermute(p, v);
    1332          35 :   if (abscmpiu(gel(p,1), 2) < 0) pari_err_DOMAIN("addprimes", "p", "<", gen_2,p);
    1333          28 :   p = addp_union(*T, p);
    1334          28 :   l = lg(p);
    1335          28 :   if (l != lg(*T))
    1336             :   {
    1337          28 :     GEN old = *T, t = cgetalloc(t_VEC, l);
    1338          28 :     for (i = 1; i < l; i++) gel(t,i) = gel(p,i);
    1339          28 :     *T = t; free(old);
    1340             :   }
    1341          28 :   avma = av; return *T;
    1342             : }
    1343             : GEN
    1344         161 : addprimes(GEN p) { return addp(&primetab, p); }
    1345             : 
    1346             : static GEN
    1347          28 : rmprimes(GEN T, GEN prime)
    1348             : {
    1349             :   long i,tx;
    1350             : 
    1351          28 :   if (!prime) return T;
    1352          28 :   tx = typ(prime);
    1353          28 :   if (is_vec_t(tx))
    1354             :   {
    1355          14 :     if (prime == T)
    1356             :     {
    1357           7 :       for (i=1; i < lg(prime); i++) gunclone(gel(prime,i));
    1358           7 :       setlg(prime, 1);
    1359             :     }
    1360             :     else
    1361             :     {
    1362           7 :       for (i=1; i < lg(prime); i++) rmprime(T, gel(prime,i));
    1363             :     }
    1364          14 :     return T;
    1365             :   }
    1366          14 :   rmprime(T, prime); return T;
    1367             : }
    1368             : GEN
    1369          28 : removeprimes(GEN prime) { return rmprimes(primetab, prime); }

Generated by: LCOV version 1.11