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 19611-73a567d) Lines: 558 619 90.1 %
Date: 2016-09-27 05:54:40 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       79113 : init_MR_Jaeschke(MR_Jaeschke_t *S, GEN n)
      33             : {
      34       79113 :   if (signe(n) < 0) n = absi(n);
      35       79113 :   S->n = n;
      36       79113 :   S->t = addsi(-1,n);
      37       79113 :   S->r1 = vali(S->t);
      38       79113 :   S->t1 = shifti(S->t, -S->r1);
      39       79113 :   S->sqrt1 = cgeti(lg(n)); S->sqrt1[1] = evalsigne(0)|evallgefint(2);
      40       79113 :   S->sqrt2 = cgeti(lg(n)); S->sqrt2[1] = evalsigne(0)|evallgefint(2);
      41       79113 : }
      42             : static void
      43     6070129 : Fl_init_MR_Jaeschke(Fl_MR_Jaeschke_t *S, ulong n)
      44             : {
      45     6070129 :   S->n = n;
      46     6070129 :   S->t = n-1;
      47     6070129 :   S->r1 = vals(S->t);
      48     6076191 :   S->t1 = S->t >> S->r1;
      49     6076191 :   S->sqrt1 = 0;
      50     6076191 :   S->sqrt2 = 0;
      51     6076191 : }
      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        6982 : MR_Jaeschke_ok(MR_Jaeschke_t *S, GEN c)
      59             : {
      60        6982 :   if (signe(S->sqrt1))
      61             :   { /* saw one earlier: compare */
      62          60 :     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        6922 :     affii(c, S->sqrt1);
      72        6922 :     affii(subii(S->n, c), S->sqrt2);
      73             :   }
      74        6982 :   return 0;
      75             : }
      76             : static int
      77     1038328 : Fl_MR_Jaeschke_ok(Fl_MR_Jaeschke_t *S, ulong c)
      78             : {
      79     1038328 :   if (S->sqrt1)
      80             :   { /* saw one earlier: compare */
      81         518 :     if (c != S->sqrt1 && c != S->sqrt2) return 1;
      82             :   } else { /* remember */
      83     1037810 :     S->sqrt1 = c;
      84     1037810 :     S->sqrt2 = S->n - c;
      85             :   }
      86     1038328 :   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       79242 : bad_for_base(MR_Jaeschke_t *S, GEN a)
      93             : {
      94       79242 :   pari_sp av = avma;
      95             :   long r;
      96       79242 :   GEN c2, c = Fp_pow(a, S->t1, S->n);
      97             : 
      98       79241 :   if (is_pm1(c) || equalii(S->t, c)) return 0;
      99             : 
     100             :   /* go fishing for -1, not for 1 (saves one squaring) */
     101      137575 :   for (r = S->r1 - 1; r; r--) /* r1 - 1 squarings */
     102             :   {
     103       74002 :     c2 = c; c = remii(sqri(c), S->n);
     104       74002 :     if (equalii(S->t, c)) return MR_Jaeschke_ok(S, c2);
     105       67020 :     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       63573 :   return 1;
     112             : }
     113             : static int
     114     6076517 : Fl_bad_for_base(Fl_MR_Jaeschke_t *S, ulong a)
     115             : {
     116             :   long r;
     117     6076517 :   ulong c2, c = Fl_powu(a, S->t1, S->n);
     118             : 
     119     6084459 :   if (c == 1 || c == S->t) return 0;
     120             : 
     121             :   /* go fishing for -1, not for 1 (saves one squaring) */
     122     6621730 :   for (r = S->r1 - 1; r; r--) /* r1 - 1 squarings */
     123             :   {
     124     3536900 :     c2 = c; c = Fl_sqr(c, S->n);
     125     3537230 :     if (c == S->t) return Fl_MR_Jaeschke_ok(S, c2);
     126             :   }
     127     3084830 :   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        1736 : Fl_MR_Jaeschke(ulong n, long k)
     195             : {
     196        1736 :   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        1736 :   if (!(n & 1)) return 0;
     204        1736 :   if (k == 16)
     205             :   { /* use smaller (faster) bases if possible */
     206           0 :     p = (n < 3215031751UL)? pr: pr+13;
     207           0 :     k = 4;
     208             :   }
     209        1736 :   else if (k == 17)
     210             :   {
     211        1736 :     p = (n < 1373653UL)? pr: pr+11;
     212        1736 :     k = 2;
     213             :   }
     214           0 :   else p = pr; /* 2,3,5,... */
     215        1736 :   Fl_init_MR_Jaeschke(&S, n);
     216        5112 :   for (i=1; i<=k; i++)
     217             :   {
     218        3424 :     r = p[i] % n; if (!r) break;
     219        3424 :     if (Fl_bad_for_base(&S, r)) return 0;
     220             :   }
     221        1688 :   return 1;
     222             : }
     223             : 
     224             : int
     225        1873 : MR_Jaeschke(GEN n, long k)
     226             : {
     227        1873 :   pari_sp av2, av = avma;
     228        1873 :   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        1873 :   if (lgefint(n) == 3) return Fl_MR_Jaeschke(uel(n,2), k);
     235             : 
     236         137 :   if (!mod2(n)) return 0;
     237         137 :   if      (k == 16) { p = pr+13; k = 4; } /* 2,13,23,1662803 */
     238         137 :   else if (k == 17) { p = pr+11; k = 2; } /* 31,73 */
     239           0 :   else p = pr; /* 2,3,5,... */
     240         137 :   init_MR_Jaeschke(&S, n); av2 = avma;
     241         381 :   for (i=1; i<=k; i++)
     242             :   {
     243         259 :     if (bad_for_base(&S, utoipos(p[i]))) { avma = av; return 0; }
     244         244 :     avma = av2;
     245             :   }
     246         122 :   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       15411 : LucasMod(GEN n, ulong P, GEN N)
     259             : {
     260       15411 :   pari_sp av = avma;
     261       15411 :   GEN nd = int_MSW(n);
     262       15411 :   ulong m = *nd;
     263             :   long i, j;
     264       15411 :   GEN v = utoipos(P), v1 = utoipos(P*P - 2);
     265             : 
     266       15411 :   if (m == 1)
     267        1128 :     j = 0;
     268             :   else
     269             :   {
     270       14283 :     j = 1+bfffo(m); /* < BIL */
     271       14283 :     m <<= j; j = BITS_IN_LONG - j;
     272             :   }
     273       15411 :   for (i=lgefint(n)-2;;) /* cf. leftright_pow */
     274             :   {
     275     1020042 :     for (; j; m<<=1,j--)
     276             :     { /* v = v_k, v1 = v_{k+1} */
     277      992356 :       if (m&HIGHBIT)
     278             :       { /* set v = v_{2k+1}, v1 = v_{2k+2} */
     279      158740 :         v = subiu(mulii(v,v1), P);
     280      158740 :         v1= subiu(sqri(v1), 2);
     281             :       }
     282             :       else
     283             :       {/* set v = v_{2k}, v1 = v_{2k+1} */
     284      833616 :         v1= subiu(mulii(v,v1), P);
     285      833616 :         v = subiu(sqri(v), 2);
     286             :       }
     287      992356 :       v = modii(v, N);
     288      992355 :       v1= modii(v1,N);
     289      992356 :       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       43097 :     if (--i == 0) return v;
     296       12275 :     j = BITS_IN_LONG;
     297       12275 :     nd=int_precW(nd);
     298       12275 :     m = *nd;
     299       12275 :   }
     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     1111334 : u_LucasMod(ulong n, ulong P, ulong N)
     306             : {
     307             :   ulong v, v1, m;
     308             :   long j;
     309             : 
     310     1111334 :   if (n == 1) return P;
     311     1111315 :   j = 1 + bfffo(n); /* < BIL */
     312     1111315 :   v = P; v1 = P*P - 2;
     313     1111315 :   m = n<<j; j = BITS_IN_LONG - j;
     314    59026374 :   for (; j; m<<=1,j--)
     315             :   { /* v = v_k, v1 = v_{k+1} */
     316    57915059 :     if (m & HIGHBIT)
     317             :     { /* set v = v_{2k+1}, v1 = v_{2k+2} */
     318     4732921 :       v = Fl_sub(Fl_mul(v,v1,N), P, N);
     319     4732921 :       v1= Fl_sub(Fl_sqr(v1,N), 2UL, N);
     320             :     }
     321             :     else
     322             :     {/* set v = v_{2k}, v1 = v_{2k+1} */
     323    53182138 :       v1= Fl_sub(Fl_mul(v,v1,N),P, N);
     324    53182138 :       v = Fl_sub(Fl_sqr(v,N), 2UL, N);
     325             :     }
     326             :   }
     327     1111315 :   return v;
     328             : }
     329             : 
     330             : int
     331     1111341 : uislucaspsp(ulong n)
     332             : {
     333             :   long i, v;
     334     1111341 :   ulong b, z, m = n + 1;
     335             : 
     336     1887904 :   for (b=3, i=0;; b+=2, i++)
     337             :   {
     338     1887904 :     ulong c = b*b - 4; /* = 1 mod 4 */
     339     1887904 :     if (krouu(n % c, c) < 0) break;
     340      776570 :     if (i == 64 && uissquareall(n, &c)) return 0; /* oo loop if N = m^2 */
     341      776563 :   }
     342     1111334 :   if (!m) return 0; /* neither 2^32-1 nor 2^64-1 are Lucas-pp */
     343     1111334 :   v = vals(m); m >>= v;
     344     1111334 :   z = u_LucasMod(m, b, n);
     345     1111334 :   if (z == 2 || z == n-2) return 1;
     346      587739 :   for (i=1; i<v; i++)
     347             :   {
     348      587695 :     if (!z) return 1;
     349      284809 :     z = Fl_sub(Fl_sqr(z,n), 2UL, n);
     350      284809 :     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       15411 : IsLucasPsP(GEN N)
     358             : {
     359       15411 :   pari_sp av = avma;
     360             :   GEN m, z;
     361             :   long i, v;
     362             :   ulong b;
     363             : 
     364       34779 :   for (b=3;; b+=2)
     365             :   {
     366       34779 :     ulong c = b*b - 4; /* = 1 mod 4 */
     367       34779 :     if (b == 129 && Z_issquare(N)) return 0; /* avoid oo loop if N = m^2 */
     368       34779 :     if (krouu(umodiu(N,c), c) < 0) break;
     369       19368 :   }
     370       15411 :   m = addis(N,1); v = vali(m); m = shifti(m,-v);
     371       15411 :   z = LucasMod(m, b, N);
     372       15411 :   if (absequaliu(z, 2)) return 1;
     373       13470 :   if (equalii(z, subiu(N,2))) return 1;
     374       14537 :   for (i=1; i<v; i++)
     375             :   {
     376       14434 :     if (!signe(z)) return 1;
     377        8019 :     z = modii(subiu(sqri(z), 2), N);
     378        8019 :     if (absequaliu(z, 2)) return 0;
     379        8019 :     if (gc_needed(av,1))
     380             :     {
     381           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"IsLucasPsP");
     382           0 :       z = gerepileupto(av, z);
     383             :     }
     384             :   }
     385         103 :   return 0;
     386             : }
     387             : 
     388             : /* assume u odd, u > 1 */
     389             : static int
     390      269725 : iu_coprime(GEN N, ulong u)
     391             : {
     392      269725 :   const ulong n = umodiu(N, u);
     393      269723 :   return (n == 1 || gcduodd(n, u) == 1);
     394             : }
     395             : /* assume u odd, u > 1 */
     396             : static int
     397    24287665 : uu_coprime(ulong n, ulong u)
     398             : {
     399    24287665 :   return gcduodd(n, u) == 1;
     400             : }
     401             : 
     402             : /* composite strong 2-pseudoprime < 1016801 whose prime divisors are > 101 */
     403             : static int
     404     2186663 : is_2_prp_101(ulong n)
     405             : {
     406     2186663 :   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     2186451 :   } return 0;
     434             : }
     435             : 
     436             : static int
     437     6066770 : u_2_prp(ulong n)
     438             : {
     439             :   Fl_MR_Jaeschke_t S;
     440     6066770 :   Fl_init_MR_Jaeschke(&S, n);
     441     6075029 :   return Fl_bad_for_base(&S, 2) == 0;
     442             : }
     443             : static int
     444     3878007 : uBPSW_psp(ulong n) { return (u_2_prp(n) && uislucaspsp(n)); }
     445             : 
     446             : int
     447    21491022 : uisprime(ulong n)
     448             : {
     449    21491022 :   if (n < 103)
     450      866067 :     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      658251 :       case 101: return 1;
     478      207816 :       default: return 0;
     479             :     }
     480    20624955 :   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    20343932 :   if (!uu_coprime(n, 16294579238595022365UL) ||
     485    14110655 :       !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     2470873 :   if (!uu_coprime(n, 4127218095UL) ||
     492     1763840 :       !uu_coprime(n, 3948078067UL) ||
     493     1599023 :       !uu_coprime(n, 1673450759UL) ||
     494     1567797 :       !uu_coprime(n, 4269855901UL)) return 0;
     495             : #endif
     496     7075850 :   if (n < 10427) return 1;
     497     6021194 :   if (n < 1016801) return !is_2_prp_101(n) && u_2_prp(n);
     498     3839386 :   return uBPSW_psp(n);
     499             : }
     500             : 
     501             : /* assume no prime divisor <= 101 */
     502             : int
     503       16481 : uisprime_101(ulong n)
     504             : {
     505       16481 :   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       25913 : uisprime_661(ulong n) { return uBPSW_psp(n); }
     513             : 
     514             : long
     515     4570912 : BPSW_psp(GEN N)
     516             : {
     517             :   pari_sp av;
     518             :   MR_Jaeschke_t S;
     519             :   int k;
     520             : 
     521     4570912 :   if (typ(N) != t_INT) pari_err_TYPE("BPSW_psp",N);
     522     4692923 :   if (signe(N) <= 0) return 0;
     523     4701326 :   if (lgefint(N) == 3) return uisprime(uel(N,2));
     524      115957 :   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       98667 :   if (!iu_coprime(N, 16294579238595022365UL) ||
     529       57284 :       !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       99276 :   if (!iu_coprime(N, 4127218095UL) ||
     536       78966 :       !iu_coprime(N, 3948078067UL) ||
     537       71788 :       !iu_coprime(N, 1673450759UL) ||
     538       58894 :       !iu_coprime(N, 4269855901UL)) return 0;
     539             : #endif
     540             :   /* no prime divisor < 103 */
     541       74584 :   av = avma;
     542       74584 :   init_MR_Jaeschke(&S, N);
     543       74584 :   k = (!bad_for_base(&S, gen_2) && IsLucasPsP(N));
     544       74584 :   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       14531 : BPSW_psp_nosmalldiv(GEN N)
     567             : {
     568             :   pari_sp av;
     569             :   MR_Jaeschke_t S;
     570       14531 :   long l = lgefint(N);
     571             :   int k;
     572             : 
     573       14531 :   if (l == 3) return uisprime_661(uel(N,2));
     574        4406 :   av = avma;
     575             :   /* N large: test for pure power, rarely succeeds, but requires < 1% of
     576             :    * compositeness test times */
     577        4406 :   if (bit_accuracy(l) > 512 && isanypower_nosmalldiv(N, &N) != 1)
     578             :   {
     579          21 :     avma = av; return 0;
     580             :   }
     581        4385 :   init_MR_Jaeschke(&S, N);
     582        4385 :   k = (!bad_for_base(&S, gen_2) && IsLucasPsP(N));
     583        4385 :   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      943078 : BPSW_isprime_small(GEN x)
     597             : {
     598      943078 :   long l = lgefint(x);
     599             : #ifdef LONG_IS_64BIT
     600      877861 :   return (l == 3);
     601             : #else
     602       65217 :   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      942676 : BPSW_isprime(GEN N)
     760             : {
     761             :   pari_sp av;
     762             :   long t;
     763             :   GEN P;
     764      942676 :   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     3598139 : gisprime(GEN x, long flag)
     775             : {
     776     3598139 :   switch (flag)
     777             :   {
     778     3598104 :     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     4307795 : 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             :   { 11037271757UL,  500000000L},
     832             :   { 22801763489UL, 1000000000L},
     833             :   { 47055833459UL, 2000000000L},
     834             :   {122430513841UL, 5000000000L},
     835             :   {200000000507UL, 8007105083L},
     836             : #endif
     837             : };
     838             : static const int prime_table_len = numberof(prime_table);
     839             : 
     840             : /* find prime closest to n in prime_table. */
     841             : static long
     842    12490580 : prime_table_closest_p(ulong n)
     843             : {
     844             :   long i;
     845    12823072 :   for (i = 1; i < prime_table_len; i++)
     846             :   {
     847    12823023 :     ulong p = prime_table[i].p;
     848    12823023 :     if (p > n)
     849             :     {
     850    12490531 :       ulong u = n - prime_table[i-1].p;
     851    12490531 :       if (p - n > u) i--;
     852    12490531 :       break;
     853             :     }
     854             :   }
     855    12490580 :   if (i == prime_table_len) i = prime_table_len - 1;
     856    12490580 :   return i;
     857             : }
     858             : 
     859             : /* return the n-th successor of prime p > 2 */
     860             : static GEN
     861          70 : prime_successor(ulong p, ulong n)
     862             : {
     863             :   forprime_t S;
     864             :   ulong i;
     865          70 :   forprime_init(&S, utoipos(p+1), NULL);
     866          70 :   for (i = 1; i < n; i++) (void)forprime_next(&S);
     867          70 :   return forprime_next(&S);
     868             : }
     869             : /* find the N-th prime */
     870             : static GEN
     871         161 : prime_table_find_n(ulong N)
     872             : {
     873             :   byteptr d;
     874         161 :   ulong n, p, maxp = maxprime();
     875             :   long i;
     876        2037 :   for (i = 1; i < prime_table_len; i++)
     877             :   {
     878        2037 :     n = prime_table[i].n;
     879        2037 :     if (n > N)
     880             :     {
     881         161 :       ulong u = N - prime_table[i-1].n;
     882         161 :       if (n - N > u) i--;
     883         161 :       break;
     884             :     }
     885             :   }
     886         161 :   if (i == prime_table_len) i = prime_table_len - 1;
     887         161 :   p = prime_table[i].p;
     888         161 :   n = prime_table[i].n;
     889         161 :   if (n > N && p > maxp)
     890             :   {
     891          14 :     i--;
     892          14 :     p = prime_table[i].p;
     893          14 :     n = prime_table[i].n;
     894             :   }
     895             :   /* if beyond prime table, then n <= N */
     896         161 :   d = diffptr + n;
     897         161 :   if (n > N)
     898             :   {
     899          14 :     n -= N;
     900       50624 :     do { n--; PREC_PRIME_VIADIFF(p,d); } while (n) ;
     901             :   }
     902         147 :   else if (n < N)
     903             :   {
     904         147 :     n = N-n;
     905         147 :     if (p > maxp) return prime_successor(p, n);
     906             :     do {
     907       44492 :       if (!*d) return prime_successor(p, n);
     908       44492 :       n--; NEXT_PRIME_VIADIFF(p,d);
     909       44492 :     } while (n) ;
     910             :   }
     911          91 :   return utoipos(p);
     912             : }
     913             : 
     914             : ulong
     915           0 : uprime(long N)
     916             : {
     917           0 :   pari_sp av = avma;
     918             :   GEN p;
     919           0 :   if (N <= 0) pari_err_DOMAIN("prime", "n", "<=",gen_0, stoi(N));
     920           0 :   p = prime_table_find_n(N);
     921           0 :   if (lgefint(p) != 3) pari_err_OVERFLOW("uprime");
     922           0 :   avma = av; return p[2];
     923             : }
     924             : GEN
     925         168 : prime(long N)
     926             : {
     927         168 :   pari_sp av = avma;
     928             :   GEN p;
     929         168 :   if (N <= 0) pari_err_DOMAIN("prime", "n", "<=",gen_0, stoi(N));
     930         161 :   new_chunk(4); /*HACK*/
     931         161 :   p = prime_table_find_n(N);
     932         161 :   avma = av; return icopy(p);
     933             : }
     934             : 
     935             : /* random b-bit prime */
     936             : GEN
     937          49 : randomprime(GEN N)
     938             : {
     939          49 :   pari_sp av = avma, av2;
     940             :   GEN a, b, d;
     941          49 :   if (!N)
     942             :     for(;;)
     943             :     {
     944          63 :       ulong p = random_bits(31);
     945          63 :       if (uisprime(p)) return utoipos(p);
     946          56 :     }
     947          42 :   switch(typ(N))
     948             :   {
     949             :     case t_INT:
     950          14 :       a = gen_2;
     951          14 :       b = subiu(N,1); /* between 2 and N-1 */
     952          14 :       d = subiu(N,2);
     953          14 :       if (signe(d) <= 0)
     954           7 :         pari_err_DOMAIN("randomprime","N", "<=", gen_2, N);
     955           7 :       break;
     956             :     case t_VEC:
     957          28 :       if (lg(N) != 3) pari_err_TYPE("randomprime",N);
     958          28 :       a = gel(N,1);
     959          28 :       b = gel(N,2);
     960          28 :       if (gcmp(b, a) < 0)
     961           7 :         pari_err_DOMAIN("randomprime","b-a", "<", gen_0, mkvec2(a,b));
     962          21 :       if (typ(a) != t_INT)
     963             :       {
     964           7 :         a = gceil(a);
     965           7 :         if (typ(a) != t_INT) pari_err_TYPE("randomprime",a);
     966             :       }
     967          21 :       if (typ(b) != t_INT)
     968             :       {
     969           7 :         b = gfloor(b);
     970           7 :         if (typ(b) != t_INT) pari_err_TYPE("randomprime",b);
     971             :       }
     972          21 :       if (cmpis(a, 2) < 0)
     973             :       {
     974           7 :         a = gen_2;
     975           7 :         d = subiu(b,1);
     976             :       }
     977             :       else
     978          14 :         d = addiu(subii(b,a), 1);
     979          21 :       if (signe(d) <= 0)
     980          14 :         pari_err_DOMAIN("randomprime","floor(b) - max(ceil(a),2)", "<",
     981             :                         gen_0, mkvec2(a,b));
     982           7 :       break;
     983             :     default:
     984           0 :       pari_err_TYPE("randomprime", N);
     985           0 :       return NULL; /*notreached*/
     986             :   }
     987          14 :   av2 = avma;
     988             :   for (;;)
     989             :   {
     990         210 :     GEN p = addii(a, randomi(d));
     991         210 :     if (BPSW_psp(p)) return gerepileuptoint(av, p);
     992         196 :     avma = av2;
     993         196 :   }
     994             : }
     995             : 
     996             : /* set *pp = nextprime(a) = p
     997             :  *     *pd so that NEXT_PRIME_VIADIFF(d, p) = nextprime(p+1)
     998             :  *     *pn so that p = the n-th prime
     999             :  * error if nextprime(a) is out of primetable bounds */
    1000             : void
    1001    12490427 : prime_table_next_p(ulong a, byteptr *pd, ulong *pp, ulong *pn)
    1002             : {
    1003             :   byteptr d;
    1004    12490427 :   ulong p, n, maxp = maxprime();
    1005    12490426 :   long i = prime_table_closest_p(a);
    1006    12490435 :   p = prime_table[i].p;
    1007    12490435 :   if (p > a && p > maxp)
    1008             :   {
    1009           0 :     i--;
    1010           0 :     p = prime_table[i].p;
    1011             :   }
    1012             :   /* if beyond prime table, then p <= a */
    1013    12490435 :   n = prime_table[i].n;
    1014    12490435 :   d = diffptr + n;
    1015    12490435 :   if (p < a)
    1016             :   {
    1017    12384394 :     if (a > maxp) pari_err_MAXPRIME(a);
    1018    27723983 :     do { n++; NEXT_PRIME_VIADIFF(p,d); } while (p < a);
    1019             :   }
    1020      106041 :   else if (p != a)
    1021             :   {
    1022     2344235 :     do { n--; PREC_PRIME_VIADIFF(p,d); } while (p > a) ;
    1023        8611 :     if (p < a) { NEXT_PRIME_VIADIFF(p,d); n++; }
    1024             :   }
    1025    12490516 :   *pn = n;
    1026    12490516 :   *pp = p;
    1027    12490516 :   *pd = d;
    1028    12490516 : }
    1029             : 
    1030             : ulong
    1031        9274 : uprimepi(ulong a)
    1032             : {
    1033        9274 :   ulong p, n, maxp = maxprime();
    1034        9274 :   if (a <= maxp)
    1035             :   {
    1036             :     byteptr d;
    1037        9149 :     prime_table_next_p(a, &d, &p, &n);
    1038        9149 :     return p == a? n: n-1;
    1039             :   }
    1040             :   else
    1041             :   {
    1042         125 :     long i = prime_table_closest_p(a);
    1043             :     forprime_t S;
    1044         125 :     p = prime_table[i].p;
    1045         125 :     if (p > a)
    1046             :     {
    1047          28 :       i--;
    1048          28 :       p = prime_table[i].p;
    1049             :     }
    1050             :     /* p = largest prime in table <= a */
    1051         125 :     n = prime_table[i].n;
    1052         125 :     (void)u_forprime_init(&S, p+1, a);
    1053         125 :     for (; p; n++) p = u_forprime_next(&S);
    1054         125 :     return n-1;
    1055             :   }
    1056             : }
    1057             : 
    1058             : GEN
    1059         252 : primepi(GEN x)
    1060             : {
    1061         252 :   pari_sp av = avma;
    1062         252 :   GEN pp, nn, N = typ(x) == t_INT? x: gfloor(x);
    1063             :   forprime_t S;
    1064             :   ulong n, p;
    1065             :   long i, l;
    1066         252 :   if (typ(N) != t_INT) pari_err_TYPE("primepi",N);
    1067         252 :   if (signe(N) <= 0) return gen_0;
    1068         252 :   avma = av; l = lgefint(N);
    1069         252 :   if (l == 3) return utoi(uprimepi(N[2]));
    1070           1 :   i = prime_table_len-1;
    1071           1 :   p = prime_table[i].p;
    1072           1 :   n = prime_table[i].n;
    1073           1 :   (void)forprime_init(&S, utoipos(p+1), N);
    1074           1 :   nn = setloop(utoipos(n));
    1075           1 :   pp = gen_0;
    1076           1 :   for (; pp; incloop(nn)) pp = forprime_next(&S);
    1077           1 :   return gerepileuptoint(av, subiu(nn,1));
    1078             : }
    1079             : 
    1080             : /* pi(x) < x/log x * (1 + 1/log x + 2.51/log^2 x)), x>=355991 [ Dusart ]
    1081             :  * pi(x) < x/(log x - 1.1), x >= 60184 [ Dusart ]
    1082             :  * ? \p9
    1083             :  * ? M = 0; for(x = 4, 60184, M = max(M, log(x) - x/primepi(x))); M
    1084             :  * %1 = 1.11196252 */
    1085             : double
    1086       54314 : primepi_upper_bound(double x)
    1087             : {
    1088       54314 :   if (x >= 355991)
    1089             :   {
    1090          14 :     double L = 1/log(x);
    1091          14 :     return x * L * (1 + L + 2.51*L*L);
    1092             :   }
    1093       54300 :   if (x >= 60184) return x / (log(x) - 1.1);
    1094       54300 :   if (x < 5) return 2; /* don't bother */
    1095       50674 :   return x / (log(x) - 1.111963);
    1096             : }
    1097             : /* pi(x) > x/log x (1 + 1/log x), x >= 599 [ Dusart ]
    1098             :  * pi(x) > x / (log x + 2), x >= 55 [ Rosser ] */
    1099             : double
    1100          14 : primepi_lower_bound(double x)
    1101             : {
    1102          14 :   if (x >= 599)
    1103             :   {
    1104          14 :     double L = 1/log(x);
    1105          14 :     return x * L * (1 + L);
    1106             :   }
    1107           0 :   if (x < 55) return 0; /* don't bother */
    1108           0 :   return x / (log(x) + 2.);
    1109             : }
    1110             : GEN
    1111           1 : gprimepi_upper_bound(GEN x)
    1112             : {
    1113           1 :   pari_sp av = avma;
    1114             :   double L;
    1115           1 :   if (typ(x) != t_INT) x = gfloor(x);
    1116           1 :   if (expi(x) <= 1022)
    1117             :   {
    1118           1 :     avma = av;
    1119           1 :     return dbltor(primepi_upper_bound(gtodouble(x)));
    1120             :   }
    1121           0 :   x = itor(x, LOWDEFAULTPREC);
    1122           0 :   L = 1 / rtodbl(logr_abs(x));
    1123           0 :   x = mulrr(x, dbltor(L * (1 + L + 2.51*L*L)));
    1124           0 :   return gerepileuptoleaf(av, x);
    1125             : }
    1126             : GEN
    1127           1 : gprimepi_lower_bound(GEN x)
    1128             : {
    1129           1 :   pari_sp av = avma;
    1130             :   double L;
    1131           1 :   if (typ(x) != t_INT) x = gfloor(x);
    1132           1 :   if (abscmpiu(x, 55) <= 0) return gen_0;
    1133           1 :   if (expi(x) <= 1022)
    1134             :   {
    1135           1 :     avma = av;
    1136           1 :     return dbltor(primepi_lower_bound(gtodouble(x)));
    1137             :   }
    1138           0 :   x = itor(x, LOWDEFAULTPREC);
    1139           0 :   L = 1 / rtodbl(logr_abs(x));
    1140           0 :   x = mulrr(x, dbltor(L * (1 + L)));
    1141           0 :   return gerepileuptoleaf(av, x);
    1142             : }
    1143             : 
    1144             : GEN
    1145          63 : primes(long n)
    1146             : {
    1147             :   forprime_t S;
    1148             :   long i;
    1149             :   GEN y;
    1150          63 :   if (n <= 0) return cgetg(1, t_VEC);
    1151          63 :   y = cgetg(n+1, t_VEC);
    1152          63 :   (void)new_chunk(3*n); /*HACK*/
    1153          63 :   u_forprime_init(&S, 2, ULONG_MAX);
    1154          63 :   avma = (pari_sp)y;
    1155          63 :   for (i = 1; i <= n; i++) gel(y, i) = utoipos( u_forprime_next(&S) );
    1156          63 :   return y;
    1157             : }
    1158             : GEN
    1159           0 : primes_zv(long n)
    1160             : {
    1161             :   forprime_t S;
    1162             :   long i;
    1163             :   GEN y;
    1164           0 :   if (n <= 0) return cgetg(1, t_VECSMALL);
    1165           0 :   y = cgetg(n+1,t_VECSMALL);
    1166           0 :   u_forprime_init(&S, 2, ULONG_MAX);
    1167           0 :   for (i = 1; i <= n; i++) y[i] =  u_forprime_next(&S);
    1168           0 :   avma = (pari_sp)y; return y;
    1169             : }
    1170             : GEN
    1171         119 : primes0(GEN N)
    1172             : {
    1173         119 :   switch(typ(N))
    1174             :   {
    1175          63 :     case t_INT: return primes(itos(N));
    1176             :     case t_VEC:
    1177          56 :       if (lg(N) == 3) return primes_interval(gel(N,1),gel(N,2));
    1178             :   }
    1179           0 :   pari_err_TYPE("primes", N);
    1180           0 :   return NULL;
    1181             : }
    1182             : 
    1183             : GEN
    1184          56 : primes_interval(GEN a, GEN b)
    1185             : {
    1186          56 :   pari_sp av = avma;
    1187             :   forprime_t S;
    1188             :   long i, n;
    1189             :   GEN y, d, p;
    1190          56 :   if (typ(a) != t_INT)
    1191             :   {
    1192           0 :     a = gceil(a);
    1193           0 :     if (typ(a) != t_INT) pari_err_TYPE("primes_interval",a);
    1194             :   }
    1195          56 :   if (typ(b) != t_INT)
    1196             :   {
    1197           7 :     b = gfloor(b);
    1198           7 :     if (typ(b) != t_INT) pari_err_TYPE("primes_interval",b);
    1199             :   }
    1200          49 :   if (signe(a) < 0) a = gen_2;
    1201          49 :   d = subii(b, a);
    1202          49 :   if (signe(d) < 0 || signe(b) <= 0) { avma = av; return cgetg(1, t_VEC); }
    1203          49 :   if (lgefint(b) == 3)
    1204             :   {
    1205          33 :     avma = av;
    1206          33 :     y = primes_interval_zv(itou(a), itou(b));
    1207          33 :     n = lg(y); settyp(y, t_VEC);
    1208          33 :     for (i = 1; i < n; i++) gel(y,i) = utoipos(y[i]);
    1209          33 :     return y;
    1210             :   }
    1211             :   /* at most d+1 primes in [a,b]. If d large, try better bound to lower
    1212             :    * memory use */
    1213          16 :   if (abscmpiu(d,100000) > 0)
    1214             :   {
    1215           1 :     GEN D = gsub(gprimepi_upper_bound(b), gprimepi_lower_bound(a));
    1216           1 :     D = ceil_safe(D);
    1217           1 :     if (cmpii(D, d) < 0) d = D;
    1218             :   }
    1219          16 :   n = itos(d)+1;
    1220          16 :   forprime_init(&S, a, b);
    1221          16 :   y = cgetg(n+1, t_VEC); i = 1;
    1222          16 :   while ((p = forprime_next(&S))) gel(y, i++) = icopy(p);
    1223          16 :   setlg(y, i); return gerepileupto(av, y);
    1224             : }
    1225             : 
    1226             : /* a <= b, at most d primes in [a,b]. Return them */
    1227             : static GEN
    1228         152 : primes_interval_i(ulong a, ulong b, ulong d)
    1229             : {
    1230         152 :   ulong p, i = 1, n = d + 1;
    1231             :   forprime_t S;
    1232         152 :   GEN y = cgetg(n+1, t_VECSMALL);
    1233         152 :   pari_sp av = avma;
    1234         152 :   u_forprime_init(&S, a, b);
    1235         152 :   while ((p = u_forprime_next(&S))) y[i++] = p;
    1236         152 :   avma = av; setlg(y, i); stackdummy((pari_sp)(y + i), (pari_sp)(y + n+1));
    1237         152 :   return y;
    1238             : }
    1239             : GEN
    1240          33 : primes_interval_zv(ulong a, ulong b)
    1241             : {
    1242             :   ulong d;
    1243          33 :   if (!a) return primes_upto_zv(b);
    1244          33 :   if (b < a) return cgetg(1, t_VECSMALL);
    1245          33 :   d = b - a;
    1246          33 :   if (d > 100000UL)
    1247             :   {
    1248          13 :     ulong D = (ulong)ceil(primepi_upper_bound(b)-primepi_lower_bound(a));
    1249          13 :     if (D < d) d = D;
    1250             :   }
    1251          33 :   return primes_interval_i(a, b, d);
    1252             : }
    1253             : GEN
    1254         119 : primes_upto_zv(ulong b)
    1255             : {
    1256             :   ulong d;
    1257         119 :   if (b < 2) return cgetg(1, t_VECSMALL);
    1258         119 :   d = (b > 100000UL)? (ulong)primepi_upper_bound(b): b;
    1259         119 :   return primes_interval_i(2, b, d);
    1260             : }
    1261             : 
    1262             : /***********************************************************************/
    1263             : /**                                                                   **/
    1264             : /**                       PRIVATE PRIME TABLE                         **/
    1265             : /**                                                                   **/
    1266             : /***********************************************************************/
    1267             : /* delete dummy NULL entries */
    1268             : static void
    1269          21 : cleanprimetab(GEN T)
    1270             : {
    1271          21 :   long i,j, l = lg(T);
    1272          70 :   for (i = j = 1; i < l; i++)
    1273          49 :     if (T[i]) T[j++] = T[i];
    1274          21 :   setlg(T,j);
    1275          21 : }
    1276             : /* remove p from T */
    1277             : static void
    1278          28 : rmprime(GEN T, GEN p)
    1279             : {
    1280             :   long i;
    1281          28 :   if (typ(p) != t_INT) pari_err_TYPE("removeprimes",p);
    1282          28 :   i = ZV_search(T, p);
    1283          28 :   if (!i)
    1284           7 :     pari_err_DOMAIN("removeprime","prime","not in",
    1285             :                     strtoGENstr("primetable"), p);
    1286          21 :   gunclone(gel(T,i)); gel(T,i) = NULL;
    1287          21 :   cleanprimetab(T);
    1288          21 : }
    1289             : 
    1290             : /*stolen from ZV_union_shallow() : clone entries from y */
    1291             : static GEN
    1292          28 : addp_union(GEN x, GEN y)
    1293             : {
    1294          28 :   long i, j, k, lx = lg(x), ly = lg(y);
    1295          28 :   GEN z = cgetg(lx + ly - 1, t_VEC);
    1296          28 :   i = j = k = 1;
    1297          63 :   while (i<lx && j<ly)
    1298             :   {
    1299           7 :     int s = cmpii(gel(x,i), gel(y,j));
    1300           7 :     if (s < 0)
    1301           0 :       gel(z,k++) = gel(x,i++);
    1302           7 :     else if (s > 0)
    1303           0 :       gel(z,k++) = gclone(gel(y,j++));
    1304             :     else {
    1305           7 :       gel(z,k++) = gel(x,i++);
    1306           7 :       j++;
    1307             :     }
    1308             :   }
    1309          28 :   while (i<lx) gel(z,k++) = gel(x,i++);
    1310          28 :   while (j<ly) gel(z,k++) = gclone(gel(y,j++));
    1311          28 :   setlg(z, k); return z;
    1312             : }
    1313             : 
    1314             : /* p is NULL, or a single element or a row vector with "primes" to add to
    1315             :  * prime table. */
    1316             : static GEN
    1317         161 : addp(GEN *T, GEN p)
    1318             : {
    1319         161 :   pari_sp av = avma;
    1320             :   long i, l;
    1321             :   GEN v;
    1322             : 
    1323         161 :   if (!p || lg(p) == 1) return *T;
    1324          42 :   if (!is_vec_t(typ(p))) p = mkvec(p);
    1325             : 
    1326          42 :   RgV_check_ZV(p, "addprimes");
    1327          35 :   v = gen_indexsort_uniq(p, (void*)&cmpii, &cmp_nodata);
    1328          35 :   p = vecpermute(p, v);
    1329          35 :   if (abscmpiu(gel(p,1), 2) < 0) pari_err_DOMAIN("addprimes", "p", "<", gen_2,p);
    1330          28 :   p = addp_union(*T, p);
    1331          28 :   l = lg(p);
    1332          28 :   if (l != lg(*T))
    1333             :   {
    1334          28 :     GEN old = *T, t = cgetalloc(t_VEC, l);
    1335          28 :     for (i = 1; i < l; i++) gel(t,i) = gel(p,i);
    1336          28 :     *T = t; free(old);
    1337             :   }
    1338          28 :   avma = av; return *T;
    1339             : }
    1340             : GEN
    1341         161 : addprimes(GEN p) { return addp(&primetab, p); }
    1342             : 
    1343             : static GEN
    1344          28 : rmprimes(GEN T, GEN prime)
    1345             : {
    1346             :   long i,tx;
    1347             : 
    1348          28 :   if (!prime) return T;
    1349          28 :   tx = typ(prime);
    1350          28 :   if (is_vec_t(tx))
    1351             :   {
    1352          14 :     if (prime == T)
    1353             :     {
    1354           7 :       for (i=1; i < lg(prime); i++) gunclone(gel(prime,i));
    1355           7 :       setlg(prime, 1);
    1356             :     }
    1357             :     else
    1358             :     {
    1359           7 :       for (i=1; i < lg(prime); i++) rmprime(T, gel(prime,i));
    1360             :     }
    1361          14 :     return T;
    1362             :   }
    1363          14 :   rmprime(T, prime); return T;
    1364             : }
    1365             : GEN
    1366          28 : removeprimes(GEN prime) { return rmprimes(primetab, prime); }

Generated by: LCOV version 1.11