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 - arith2.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 20443-183d202) Lines: 554 598 92.6 %
Date: 2017-03-27 05:17:48 Functions: 79 82 96.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             : /*********************************************************************/
      15             : /**                                                                 **/
      16             : /**                     ARITHMETIC FUNCTIONS                        **/
      17             : /**                        (second part)                            **/
      18             : /**                                                                 **/
      19             : /*********************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : 
      23             : GEN
      24          25 : boundfact(GEN n, ulong lim)
      25             : {
      26          25 :   switch(typ(n))
      27             :   {
      28          15 :     case t_INT: return Z_factor_limit(n,lim);
      29             :     case t_FRAC: {
      30          10 :       pari_sp av = avma;
      31          10 :       GEN a = Z_factor_limit(gel(n,1),lim);
      32          10 :       GEN b = Z_factor_limit(gel(n,2),lim);
      33          10 :       gel(b,2) = ZC_neg(gel(b,2));
      34          10 :       return gerepilecopy(av, merge_factor(a,b,(void*)&cmpii,cmp_nodata));
      35             :     }
      36             :   }
      37           0 :   pari_err_TYPE("boundfact",n);
      38             :   return NULL; /* LCOV_EXCL_LINE */
      39             : }
      40             : 
      41             : /* NOT memory clean */
      42             : GEN
      43        6270 : Z_smoothen(GEN N, GEN L, GEN *pP, GEN *pe)
      44             : {
      45        6270 :   long i, j, l = lg(L);
      46        6270 :   GEN e = new_chunk(l), P = new_chunk(l);
      47       59750 :   for (i = j = 1; i < l; i++)
      48             :   {
      49       56985 :     ulong p = uel(L,i);
      50       56985 :     long v = Z_lvalrem(N, p, &N);
      51       56985 :     if (v) { P[j] = p; e[j] = v; j++; if (is_pm1(N)) { N = NULL; break; } }
      52             :   }
      53        6270 :   P[0] = evaltyp(t_VECSMALL) | evallg(j); *pP = P;
      54        6270 :   e[0] = evaltyp(t_VECSMALL) | evallg(j); *pe = e; return N;
      55             : }
      56             : /***********************************************************************/
      57             : /**                                                                   **/
      58             : /**                    SIMPLE FACTORISATIONS                          **/
      59             : /**                                                                   **/
      60             : /***********************************************************************/
      61             : /* Factor n and output [p,e,c] where
      62             :  * p, e and c are vecsmall with n = prod{p[i]^e[i]} and c[i] = p[i]^e[i] */
      63             : GEN
      64       19620 : factoru_pow(ulong n)
      65             : {
      66       19620 :   GEN f = cgetg(4,t_VEC);
      67       19620 :   pari_sp av = avma;
      68             :   GEN F, P, E, p, e, c;
      69             :   long i, l;
      70             :   /* enough room to store <= 15 * [p,e,p^e] (OK if n < 2^64) */
      71       19620 :   (void)new_chunk((15 + 1)*3);
      72       19620 :   F = factoru(n);
      73       19620 :   P = gel(F,1);
      74       19620 :   E = gel(F,2); l = lg(P);
      75       19620 :   avma = av;
      76       19620 :   gel(f,1) = p = cgetg(l,t_VECSMALL);
      77       19620 :   gel(f,2) = e = cgetg(l,t_VECSMALL);
      78       19620 :   gel(f,3) = c = cgetg(l,t_VECSMALL);
      79       61950 :   for(i = 1; i < l; i++)
      80             :   {
      81       42330 :     p[i] = P[i];
      82       42330 :     e[i] = E[i];
      83       42330 :     c[i] = upowuu(p[i], e[i]);
      84             :   }
      85       19620 :   return f;
      86             : }
      87             : 
      88             : static GEN
      89       57415 : factorlim(GEN n, ulong lim)
      90       57415 : { return lim? Z_factor_limit(n, lim): Z_factor(n); }
      91             : /* factor p^n - 1, assuming p prime. If lim != 0, limit factorization to
      92             :  * primes <= lim */
      93             : GEN
      94       44565 : factor_pn_1_limit(GEN p, long n, ulong lim)
      95             : {
      96       44565 :   pari_sp av = avma;
      97       44565 :   GEN A = factorlim(subiu(p,1), lim), d = divisorsu(n);
      98       44565 :   long i, pp = itos_or_0(p);
      99       53705 :   for(i=2; i<lg(d); i++)
     100             :   {
     101             :     GEN B;
     102       16900 :     if (pp && d[i]%pp==0 && (
     103       15520 :        ((pp&3)==1 && (d[i]&1)) ||
     104        7825 :        ((pp&3)==3 && (d[i]&3)==2) ||
     105        7685 :        (pp==2 && (d[i]&7)==4)))
     106        3710 :     {
     107        3710 :       GEN f=factor_Aurifeuille_prime(p,d[i]);
     108        3710 :       B = factorlim(f, lim);
     109        3710 :       A = merge_factor(A, B, (void*)&cmpii, cmp_nodata);
     110        3710 :       B = factorlim(diviiexact(polcyclo_eval(d[i],p), f), lim);
     111             :     }
     112             :     else
     113        5430 :       B = factorlim(polcyclo_eval(d[i],p), lim);
     114        9140 :     A = merge_factor(A, B, (void*)&cmpii, cmp_nodata);
     115             :   }
     116       44565 :   return gerepilecopy(av, A);
     117             : }
     118             : GEN
     119       44565 : factor_pn_1(GEN p, ulong n)
     120       44565 : { return factor_pn_1_limit(p, n, 0); }
     121             : 
     122             : #if 0
     123             : static GEN
     124             : to_mat(GEN p, long e) {
     125             :   GEN B = cgetg(3, t_MAT);
     126             :   gel(B,1) = mkcol(p);
     127             :   gel(B,2) = mkcol(utoipos(e)); return B;
     128             : }
     129             : /* factor phi(n) */
     130             : GEN
     131             : factor_eulerphi(GEN n)
     132             : {
     133             :   pari_sp av = avma;
     134             :   GEN B = NULL, A, P, E, AP, AE;
     135             :   long i, l, v = vali(n);
     136             : 
     137             :   l = lg(n);
     138             :   /* result requires less than this: at most expi(n) primes */
     139             :   (void)new_chunk(bit_accuracy(l) * (l /*p*/ + 3 /*e*/ + 2 /*vectors*/) + 3+2);
     140             :   if (v) { n = shifti(n, -v); v--; }
     141             :   A = Z_factor(n); P = gel(A,1); E = gel(A,2); l = lg(P);
     142             :   for(i = 1; i < l; i++)
     143             :   {
     144             :     GEN p = gel(P,i), q = subiu(p,1), fa;
     145             :     long e = itos(gel(E,i)), w;
     146             : 
     147             :     w = vali(q); v += w; q = shifti(q,-w);
     148             :     if (! is_pm1(q))
     149             :     {
     150             :       fa = Z_factor(q);
     151             :       B = B? merge_factor(B, fa, (void*)&cmpii, cmp_nodata): fa;
     152             :     }
     153             :     if (e > 1) {
     154             :       if (B) {
     155             :         gel(B,1) = shallowconcat(gel(B,1), p);
     156             :         gel(B,2) = shallowconcat(gel(B,2), utoipos(e-1));
     157             :       } else
     158             :         B = to_mat(p, e-1);
     159             :     }
     160             :   }
     161             :   avma = av;
     162             :   if (!B) return v? to_mat(gen_2, v): trivial_fact();
     163             :   A = cgetg(3, t_MAT);
     164             :   P = gel(B,1); E = gel(B,2); l = lg(P);
     165             :   AP = cgetg(l+1, t_COL); gel(A,1) = AP; AP++;
     166             :   AE = cgetg(l+1, t_COL); gel(A,2) = AE; AE++;
     167             :   /* prepend "2^v" */
     168             :   gel(AP,0) = gen_2;
     169             :   gel(AE,0) = utoipos(v);
     170             :   for (i = 1; i < l; i++)
     171             :   {
     172             :     gel(AP,i) = icopy(gel(P,i));
     173             :     gel(AE,i) = icopy(gel(E,i));
     174             :   }
     175             :   return A;
     176             : }
     177             : #endif
     178             : 
     179             : /***********************************************************************/
     180             : /**                                                                   **/
     181             : /**         CHECK FACTORIZATION FOR ARITHMETIC FUNCTIONS              **/
     182             : /**                                                                   **/
     183             : /***********************************************************************/
     184             : static int
     185     2263561 : RgV_is_ZVpos(GEN v)
     186             : {
     187     2263561 :   long i, l = lg(v);
     188     6952662 :   for (i = 1; i < l; i++)
     189             :   {
     190     4689106 :     GEN c = gel(v,i);
     191     4689106 :     if (typ(c) != t_INT || signe(c) <= 0) return 0;
     192             :   }
     193     2263556 :   return 1;
     194             : }
     195             : /* check whether v is a ZV with non-0 entries */
     196             : static int
     197        3690 : RgV_is_ZVnon0(GEN v)
     198             : {
     199        3690 :   long i, l = lg(v);
     200       11275 :   for (i = 1; i < l; i++)
     201             :   {
     202        7620 :     GEN c = gel(v,i);
     203        7620 :     if (typ(c) != t_INT || !signe(c)) return 0;
     204             :   }
     205        3655 :   return 1;
     206             : }
     207             : /* check whether v is a ZV with non-zero entries OR exactly [0] */
     208             : static int
     209        2305 : RgV_is_ZV0(GEN v)
     210             : {
     211        2305 :   long i, l = lg(v);
     212        6805 :   for (i = 1; i < l; i++)
     213             :   {
     214        4565 :     GEN c = gel(v,i);
     215             :     long s;
     216        4565 :     if (typ(c) != t_INT) return 0;
     217        4565 :     s = signe(c);
     218        4565 :     if (!s) return (l == 2);
     219             :   }
     220        2240 :   return 1;
     221             : }
     222             : 
     223             : static int
     224        1895 : RgV_is_prV(GEN v)
     225             : {
     226        1895 :   long l = lg(v), i;
     227        2145 :   for (i = 1; i < l; i++)
     228        1895 :     if (!checkprid_i(gel(v,i))) return 0;
     229         250 :   return 1;
     230             : }
     231             : int
     232        4600 : is_nf_factor(GEN F)
     233             : {
     234       12905 :   return typ(F) == t_MAT && lg(F) == 3
     235        6490 :     && RgV_is_prV(gel(F,1)) && RgV_is_ZVpos(gel(F,2));
     236             : }
     237             : int
     238          10 : is_nf_extfactor(GEN F)
     239             : {
     240          30 :   return typ(F) == t_MAT && lg(F) == 3
     241          15 :     && RgV_is_prV(gel(F,1)) && RgV_is_ZV(gel(F,2));
     242             : }
     243             : 
     244             : static int
     245     1134663 : is_Z_factor_i(GEN f)
     246     1134663 : { return typ(f) == t_MAT && lg(f) == 3 && RgV_is_ZVpos(gel(f,2)); }
     247             : int
     248     1128668 : is_Z_factorpos(GEN f)
     249     1128668 : { return is_Z_factor_i(f) && RgV_is_ZVpos(gel(f,1)); }
     250             : int
     251        2305 : is_Z_factor(GEN f)
     252        2305 : { return is_Z_factor_i(f) && RgV_is_ZV0(gel(f,1)); }
     253             : /* as is_Z_factorpos, also allow factor(0) */
     254             : int
     255        3690 : is_Z_factornon0(GEN f)
     256        3690 : { return is_Z_factor_i(f) && RgV_is_ZVnon0(gel(f,1)); }
     257             : GEN
     258        3245 : clean_Z_factor(GEN f)
     259             : {
     260        3245 :   GEN P = gel(f,1);
     261        3245 :   long n = lg(P)-1;
     262        3245 :   if (n && equalim1(gel(P,1)))
     263        1540 :     return mkmat2(vecslice(P,2,n), vecslice(gel(f,2),2,n));
     264        1705 :   return f;
     265             : }
     266             : GEN
     267           0 : fuse_Z_factor(GEN f, GEN B)
     268             : {
     269           0 :   GEN P = gel(f,1), E = gel(f,2), P2,E2;
     270           0 :   long i, l = lg(P);
     271           0 :   if (l == 1) return f;
     272           0 :   for (i = 1; i < l; i++)
     273           0 :     if (abscmpii(gel(P,i), B) > 0) break;
     274           0 :   if (i == l) return f;
     275             :   /* tail / initial segment */
     276           0 :   P2 = vecslice(P, i, l-1); P = vecslice(P, 1, i-1);
     277           0 :   E2 = vecslice(E, i, l-1); E = vecslice(E, 1, i-1);
     278           0 :   P = shallowconcat(P, mkvec(factorback2(P2,E2)));
     279           0 :   E = shallowconcat(E, mkvec(gen_1));
     280           0 :   return mkmat2(P, E);
     281             : }
     282             : 
     283             : /* n attached to a factorization of a positive integer: either N (t_INT)
     284             :  * a factorization matrix faN, or a t_VEC: [N, faN] */
     285             : GEN
     286         135 : check_arith_pos(GEN n, const char *f) {
     287         135 :   switch(typ(n))
     288             :   {
     289             :     case t_INT:
     290         135 :       if (signe(n) <= 0) pari_err_DOMAIN(f, "argument", "<=", gen_0, gen_0);
     291         135 :       return NULL;
     292             :     case t_VEC:
     293           0 :       if (lg(n) != 3 || typ(gel(n,1)) != t_INT || signe(gel(n,1)) <= 0) break;
     294           0 :       n = gel(n,2); /* fall through */
     295             :     case t_MAT:
     296           0 :       if (!is_Z_factorpos(n)) break;
     297           0 :       return n;
     298             :   }
     299           0 :   pari_err_TYPE(f,n);
     300           0 :   return NULL;
     301             : }
     302             : /* n attached to a factorization of a non-0 integer */
     303             : GEN
     304        9505 : check_arith_non0(GEN n, const char *f) {
     305        9505 :   switch(typ(n))
     306             :   {
     307             :     case t_INT:
     308        5850 :       if (!signe(n)) pari_err_DOMAIN(f, "argument", "=", gen_0, gen_0);
     309        5810 :       return NULL;
     310             :     case t_VEC:
     311           5 :       if (lg(n) != 3 || typ(gel(n,1)) != t_INT || !signe(gel(n,1))) break;
     312           5 :       n = gel(n,2); /* fall through */
     313             :     case t_MAT:
     314        3655 :       if (!is_Z_factornon0(n)) break;
     315        3620 :       return n;
     316             :   }
     317          35 :   pari_err_TYPE(f,n);
     318           0 :   return NULL;
     319             : }
     320             : /* n attached to a factorization of an integer */
     321             : GEN
     322      130965 : check_arith_all(GEN n, const char *f) {
     323      130965 :   switch(typ(n))
     324             :   {
     325             :     case t_INT:
     326      128660 :       return NULL;
     327             :     case t_VEC:
     328         120 :       if (lg(n) != 3 || typ(gel(n,1)) != t_INT) break;
     329         120 :       n = gel(n,2); /* fall through */
     330             :     case t_MAT:
     331        2305 :       if (!is_Z_factor(n)) break;
     332        2305 :       return n;
     333             :   }
     334           0 :   pari_err_TYPE(f,n);
     335           0 :   return NULL;
     336             : }
     337             : 
     338             : /***********************************************************************/
     339             : /**                                                                   **/
     340             : /**                MISCELLANEOUS ARITHMETIC FUNCTIONS                 **/
     341             : /**                (ultimately depend on Z_factor())                  **/
     342             : /**                                                                   **/
     343             : /***********************************************************************/
     344             : /* set P,E from F. Check whether F is an integer and kill "factor" -1 */
     345             : static void
     346      250535 : set_fact_check(GEN F, GEN *pP, GEN *pE, int *isint)
     347             : {
     348             :   GEN E, P;
     349      250535 :   if (lg(F) != 3) pari_err_TYPE("divisors",F);
     350      250535 :   P = gel(F,1);
     351      250535 :   E = gel(F,2);
     352      250535 :   RgV_check_ZV(E, "divisors");
     353      250535 :   *isint = RgV_is_ZV(P);
     354      250535 :   if (*isint)
     355             :   {
     356      250525 :     long i, l = lg(P);
     357             :     /* skip -1 */
     358      250525 :     if (l>1 && signe(gel(P,1)) < 0) { E++; P = vecslice(P,2,--l); }
     359             :     /* test for 0 */
     360      816525 :     for (i = 1; i < l; i++)
     361      566005 :       if (!signe(gel(P,i)) && signe(gel(E,i)))
     362           5 :         pari_err_DOMAIN("divisors", "argument", "=", gen_0, F);
     363             :   }
     364      250530 :   *pP = P;
     365      250530 :   *pE = E;
     366      250530 : }
     367             : static void
     368        7625 : set_fact(GEN F, GEN *pP, GEN *pE) { *pP = gel(F,1); *pE = gel(F,2); }
     369             : 
     370             : int
     371      258165 : divisors_init(GEN n, GEN *pP, GEN *pE)
     372             : {
     373             :   long i,l;
     374             :   GEN E, P, e;
     375             :   int isint;
     376             : 
     377      258165 :   switch(typ(n))
     378             :   {
     379             :     case t_INT:
     380        7610 :       if (!signe(n)) pari_err_DOMAIN("divisors", "argument", "=", gen_0, gen_0);
     381        7610 :       set_fact(absZ_factor(n), &P,&E);
     382        7610 :       isint = 1; break;
     383             :     case t_VEC:
     384          10 :       if (lg(n) != 3 || typ(gel(n,2)) !=t_MAT) pari_err_TYPE("divisors",n);
     385           5 :       set_fact_check(gel(n,2), &P,&E, &isint);
     386           5 :       break;
     387             :     case t_MAT:
     388      250530 :       set_fact_check(n, &P,&E, &isint);
     389      250525 :       break;
     390             :     default:
     391          15 :       set_fact(factor(n), &P,&E);
     392          15 :       isint = 0; break;
     393             :   }
     394      258155 :   l = lg(P);
     395      258155 :   e = cgetg(l, t_VECSMALL);
     396      845975 :   for (i=1; i<l; i++)
     397             :   {
     398      587825 :     e[i] = itos(gel(E,i));
     399      587825 :     if (e[i] < 0) pari_err_TYPE("divisors [denominator]",n);
     400             :   }
     401      258150 :   *pP = P; *pE = e; return isint;
     402             : }
     403             : 
     404             : GEN
     405      258135 : divisors(GEN n)
     406             : {
     407      258135 :   pari_sp av = avma;
     408             :   long i, j, l;
     409             :   ulong ndiv;
     410             :   GEN *d, *t, *t1, *t2, *t3, P, E, e;
     411      258135 :   int isint = divisors_init(n, &P, &E);
     412             : 
     413      258120 :   l = lg(E); e = cgetg(l, t_VECSMALL);
     414      258120 :   for (i=1; i<l; i++) e[i] = E[i]+1;
     415      258120 :   ndiv = itou_or_0( zv_prod_Z(e) );
     416      258120 :   if (!ndiv || ndiv & ~LGBITS) pari_err_OVERFLOW("divisors");
     417      258120 :   d = t = (GEN*) cgetg(ndiv+1,t_VEC);
     418      258120 :   *++d = gen_1;
     419      258120 :   if (isint)
     420             :   {
     421      845790 :     for (i=1; i<l; i++)
     422     1578590 :       for (t1=t,j=E[i]; j; j--,t1=t2)
     423      990910 :         for (t2=d, t3=t1; t3<t2; ) *++d = mulii(*++t3, gel(P,i));
     424      258110 :     e = ZV_sort((GEN)t);
     425             :   } else {
     426          40 :     for (i=1; i<l; i++)
     427         130 :       for (t1=t,j=E[i]; j; j--,t1=t2)
     428         100 :         for (t2=d, t3=t1; t3<t2; ) *++d = gmul(*++t3, gel(P,i));
     429          10 :     e = (GEN)t;
     430             :   }
     431      258120 :   return gerepileupto(av, e);
     432             : }
     433             : 
     434             : GEN
     435       72350 : divisorsu_fact(GEN P, GEN E)
     436             : {
     437       72350 :   long i, j, l = lg(P);
     438       72350 :   ulong nbdiv = 1;
     439             :   GEN d, t, t1, t2, t3;
     440       72350 :   for (i=1; i<l; i++) nbdiv *= 1+E[i];
     441       72350 :   d = t = cgetg(nbdiv+1,t_VECSMALL);
     442       72350 :   *++d = 1;
     443      115785 :   for (i=1; i<l; i++)
     444       99975 :     for (t1=t,j=E[i]; j; j--,t1=t2)
     445       56540 :       for (t2=d, t3=t1; t3<t2; ) *(++d) = *(++t3) * P[i];
     446       72350 :   vecsmall_sort(t); return t;
     447             : }
     448             : GEN
     449       71480 : divisorsu(ulong n)
     450             : {
     451       71480 :   pari_sp av = avma;
     452       71480 :   GEN fa = factoru(n);
     453       71480 :   return gerepileupto(av, divisorsu_fact(gel(fa,1), gel(fa,2)));
     454             : }
     455             : 
     456             : static GEN
     457           0 : corefa(GEN fa)
     458             : {
     459           0 :   GEN P = gel(fa,1), E = gel(fa,2), c = gen_1;
     460             :   long i;
     461           0 :   for (i=1; i<lg(P); i++)
     462           0 :     if (mod2(gel(E,i))) c = mulii(c,gel(P,i));
     463           0 :   return c;
     464             : }
     465             : static GEN
     466         550 : core2fa(GEN fa)
     467             : {
     468         550 :   GEN P = gel(fa,1), E = gel(fa,2), c = gen_1, f = gen_1;
     469             :   long i;
     470        1265 :   for (i=1; i<lg(P); i++)
     471             :   {
     472         715 :     long e = itos(gel(E,i));
     473         715 :     if (e & 1)  c = mulii(c, gel(P,i));
     474         715 :     if (e != 1) f = mulii(f, powiu(gel(P,i), e >> 1));
     475             :   }
     476         550 :   return mkvec2(c,f);
     477             : }
     478             : GEN
     479           0 : corepartial(GEN n, long all)
     480             : {
     481           0 :   pari_sp av = avma;
     482           0 :   if (typ(n) != t_INT) pari_err_TYPE("corepartial",n);
     483           0 :   return gerepileuptoint(av, corefa(Z_factor_limit(n,all)));
     484             : }
     485             : GEN
     486         550 : core2partial(GEN n, long all)
     487             : {
     488         550 :   pari_sp av = avma;
     489         550 :   if (typ(n) != t_INT) pari_err_TYPE("core2partial",n);
     490         550 :   return gerepilecopy(av, core2fa(Z_factor_limit(n,all)));
     491             : }
     492             : static GEN
     493        1060 : core2_i(GEN n)
     494             : {
     495        1060 :   GEN f = core(n);
     496        1060 :   if (!signe(f)) return mkvec2(gen_0, gen_1);
     497        1035 :   switch(typ(n))
     498             :   {
     499           5 :     case t_VEC: n = gel(n,1); break;
     500         515 :     case t_MAT: n = factorback(n); break;
     501             :   }
     502        1035 :   return mkvec2(f, sqrtint(diviiexact(n, f)));
     503             : }
     504             : GEN
     505        1055 : core2(GEN n) { pari_sp av = avma; return gerepilecopy(av, core2_i(n)); }
     506             : 
     507             : GEN
     508        2210 : core0(GEN n,long flag) { return flag? core2(n): core(n); }
     509             : 
     510             : static long
     511          10 : _mod4(GEN c) {
     512          10 :   long r, s = signe(c);
     513          10 :   if (!s) return 0;
     514          10 :   r = mod4(c); if (s < 0) r = 4-r;
     515          10 :   return r;
     516             : }
     517             : 
     518             : long
     519        1970 : corediscs(long D, ulong *f)
     520             : {
     521             :   /* D = f^2 dK */
     522        1970 :   long dK = D>=0 ? (long) coreu(D) : -(long) coreu(-(ulong) D);
     523        1970 :   ulong dKmod4 = ((ulong)dK)&3UL;
     524        1970 :   if (dKmod4 == 2 || dKmod4 == 3)
     525         165 :     dK *= 4;
     526        1970 :   if (f) *f = usqrt((ulong)(D/dK));
     527        1970 :   return D;
     528             : }
     529             : 
     530             : GEN
     531           5 : coredisc(GEN n)
     532             : {
     533           5 :   pari_sp av = avma;
     534           5 :   GEN c = core(n);
     535           5 :   if (_mod4(c)<=1) return c; /* c = 0 or 1 mod 4 */
     536           5 :   return gerepileuptoint(av, shifti(c,2));
     537             : }
     538             : 
     539             : GEN
     540           5 : coredisc2(GEN n)
     541             : {
     542           5 :   pari_sp av = avma;
     543           5 :   GEN y = core2_i(n);
     544           5 :   GEN c = gel(y,1), f = gel(y,2);
     545           5 :   if (_mod4(c)<=1) return gerepilecopy(av, y);
     546           5 :   y = cgetg(3,t_VEC);
     547           5 :   gel(y,1) = shifti(c,2);
     548           5 :   gel(y,2) = gmul2n(f,-1); return gerepileupto(av, y);
     549             : }
     550             : 
     551             : GEN
     552          10 : coredisc0(GEN n,long flag) { return flag? coredisc2(n): coredisc(n); }
     553             : 
     554             : long
     555         585 : omegau(ulong n)
     556             : {
     557             :   pari_sp av;
     558             :   GEN F;
     559         585 :   if (n == 1UL) return 0;
     560         575 :   av = avma; F = factoru(n);
     561         575 :   avma = av; return lg(gel(F,1))-1;
     562             : }
     563             : long
     564        1135 : omega(GEN n)
     565             : {
     566             :   pari_sp av;
     567             :   GEN F, P;
     568        1135 :   if ((F = check_arith_non0(n,"omega"))) {
     569             :     long n;
     570         515 :     P = gel(F,1); n = lg(P)-1;
     571         515 :     if (n && equalim1(gel(P,1))) n--;
     572         515 :     return n;
     573             :   }
     574         610 :   if (lgefint(n) == 3) return omegau(n[2]);
     575          25 :   av = avma;
     576          25 :   F = absZ_factor(n);
     577          25 :   P = gel(F,1); avma = av; return lg(P)-1;
     578             : }
     579             : 
     580             : long
     581         590 : bigomegau(ulong n)
     582             : {
     583             :   pari_sp av;
     584             :   GEN F;
     585         590 :   if (n == 1) return 0;
     586         580 :   av = avma; F = factoru(n);
     587         580 :   avma = av; return zv_sum(gel(F,2));
     588             : }
     589             : long
     590        1135 : bigomega(GEN n)
     591             : {
     592        1135 :   pari_sp av = avma;
     593             :   GEN F, E;
     594        1135 :   if ((F = check_arith_non0(n,"bigomega")))
     595             :   {
     596         515 :     GEN P = gel(F,1);
     597         515 :     long n = lg(P)-1;
     598         515 :     E = gel(F,2);
     599         515 :     if (n && equalim1(gel(P,1))) E = vecslice(E,2,n);
     600             :   }
     601         610 :   else if (lgefint(n) == 3)
     602         590 :     return bigomegau(n[2]);
     603             :   else
     604          20 :     E = gel(absZ_factor(n), 2);
     605         535 :   E = ZV_to_zv(E);
     606         535 :   avma = av; return zv_sum(E);
     607             : }
     608             : 
     609             : /* assume f = factoru(n), possibly with 0 exponents. Return phi(n) */
     610             : ulong
     611      108085 : eulerphiu_fact(GEN f)
     612             : {
     613      108085 :   GEN P = gel(f,1), E = gel(f,2);
     614      108085 :   long i, m = 1, l = lg(P);
     615      425680 :   for (i = 1; i < l; i++)
     616             :   {
     617      317595 :     ulong p = P[i], e = E[i];
     618      317595 :     if (!e) continue;
     619      317595 :     if (p == 2)
     620       81420 :     { if (e > 1) m <<= e-1; }
     621             :     else
     622             :     {
     623      236175 :       m *= (p-1);
     624      236175 :       if (e > 1) m *= upowuu(p, e-1);
     625             :     }
     626             :   }
     627      108085 :   return m;
     628             : }
     629             : ulong
     630      107100 : eulerphiu(ulong n)
     631             : {
     632      107100 :   pari_sp av = avma;
     633             :   GEN F;
     634      107100 :   if (!n) return 2;
     635      107100 :   F = factoru(n);
     636      107100 :   avma = av; return eulerphiu_fact(F);
     637             : }
     638             : GEN
     639      104005 : eulerphi(GEN n)
     640             : {
     641      104005 :   pari_sp av = avma;
     642             :   GEN Q, F, P, E;
     643             :   long i, l;
     644             : 
     645      104005 :   if ((F = check_arith_all(n,"eulerphi")))
     646             :   {
     647         525 :     F = clean_Z_factor(F);
     648         525 :     n = (typ(n) == t_VEC)? gel(n,1): factorback(F);
     649         525 :     if (lgefint(n) == 3)
     650             :     {
     651             :       ulong e;
     652         515 :       F = mkmat2(ZV_to_nv(gel(F,1)), ZV_to_nv(gel(F,2)));
     653         515 :       e = eulerphiu_fact(F);
     654         515 :       avma = av; return utoipos(e);
     655             :     }
     656             :   }
     657      103480 :   else if (lgefint(n) == 3) return utoipos(eulerphiu(uel(n,2)));
     658             :   else
     659          35 :     F = absZ_factor(n);
     660          45 :   if (!signe(n)) return gen_2;
     661          25 :   P = gel(F,1);
     662          25 :   E = gel(F,2); l = lg(P);
     663          25 :   Q = cgetg(l, t_VEC);
     664         160 :   for (i = 1; i < l; i++)
     665             :   {
     666         135 :     GEN p = gel(P,i), q;
     667         135 :     ulong v = itou(gel(E,i));
     668         135 :     q = subiu(p,1);
     669         135 :     if (v != 1) q = mulii(q, v == 2? p: powiu(p, v-1));
     670         135 :     gel(Q,i) = q;
     671             :   }
     672          25 :   return gerepileuptoint(av, ZV_prod(Q));
     673             : }
     674             : 
     675             : static GEN
     676         540 : numdiv_aux(GEN F)
     677             : {
     678         540 :   GEN x, E = gel(F,2);
     679         540 :   long i, l = lg(E);
     680         540 :   x = cgetg(l, t_VECSMALL);
     681         540 :   for (i=1; i<l; i++) x[i] = itou(gel(E,i))+1;
     682         540 :   return x;
     683             : }
     684             : GEN
     685        1135 : numdiv(GEN n)
     686             : {
     687        1135 :   pari_sp av = avma;
     688             :   GEN F, E;
     689             :   long i, l;
     690        1135 :   if ((F = check_arith_non0(n,"numdiv")))
     691             :   {
     692         515 :     F = clean_Z_factor(F);
     693         515 :     E = numdiv_aux(F);
     694             :   }
     695         610 :   else if (lgefint(n) == 3)
     696             :   {
     697         585 :     if (n[2] == 1) return gen_1;
     698         575 :     F = factoru(n[2]);
     699         575 :     E = gel(F,2); l = lg(E);
     700         575 :     for (i=1; i<l; i++) E[i]++;
     701             :   }
     702             :   else
     703          25 :     E = numdiv_aux(absZ_factor(n));
     704        1115 :   return gerepileuptoint(av, zv_prod_Z(E));
     705             : }
     706             : 
     707             : /* 1 + p + ... + p^v, p != 2^BIL - 1 */
     708             : static GEN
     709        1185 : u_euler_sumdiv(ulong p, long v)
     710             : {
     711        1185 :   GEN u = utoipos(1 + p); /* can't overflow */
     712        1185 :   for (; v > 1; v--) u = addui(1, mului(p, u));
     713        1185 :   return u;
     714             : }
     715             : /* 1 + q + ... + q^v */
     716             : static GEN
     717        7455 : euler_sumdiv(GEN q, long v)
     718             : {
     719        7455 :   GEN u = addui(1, q);
     720        7455 :   for (; v > 1; v--) u = addui(1, mulii(q, u));
     721        7455 :   return u;
     722             : }
     723             : 
     724             : static GEN
     725         535 : sumdiv_aux(GEN F)
     726             : {
     727         535 :   GEN x, P = gel(F,1), E = gel(F,2);
     728         535 :   long i, l = lg(P);
     729         535 :   x = cgetg(l, t_VEC);
     730         535 :   for (i=1; i<l; i++) gel(x,i) = euler_sumdiv(gel(P,i), itou(gel(E,i)));
     731         535 :   return ZV_prod(x);
     732             : }
     733             : GEN
     734        1135 : sumdiv(GEN n)
     735             : {
     736        1135 :   pari_sp av = avma;
     737             :   GEN F, v;
     738             : 
     739        1135 :   if ((F = check_arith_non0(n,"sumdiv")))
     740             :   {
     741         515 :     F = clean_Z_factor(F);
     742         515 :     v = sumdiv_aux(F);
     743             :   }
     744         610 :   else if (lgefint(n) == 3)
     745             :   {
     746         590 :     if (n[2] == 1) return gen_1;
     747         580 :     F = factoru(n[2]);
     748         580 :     v = usumdiv_fact(F);
     749             :   }
     750             :   else
     751          20 :     v = sumdiv_aux(absZ_factor(n));
     752        1115 :   return gerepileuptoint(av, v);
     753             : }
     754             : 
     755             : static GEN
     756        1070 : sumdivk_aux(GEN F, long k)
     757             : {
     758        1070 :   GEN x, P = gel(F,1), E = gel(F,2);
     759        1070 :   long i, l = lg(P);
     760        1070 :   x = cgetg(l, t_VEC);
     761        1070 :   for (i=1; i<l; i++) gel(x,i) = euler_sumdiv(powiu(gel(P,i),k), gel(E,i)[2]);
     762        1070 :   return ZV_prod(x);
     763             : }
     764             : GEN
     765        4675 : sumdivk(GEN n, long k)
     766             : {
     767        4675 :   pari_sp av = avma;
     768             :   GEN F, v;
     769             :   long k1;
     770             : 
     771        4675 :   if (!k) return numdiv(n);
     772        4675 :   if (k == 1) return sumdiv(n);
     773        3540 :   if (k ==-1) return gerepileupto(av, gdiv(sumdiv(n), n));
     774        3540 :   k1 = k;
     775        3540 :   if (k < 0)  k = -k;
     776        3540 :   if ((F = check_arith_non0(n,"sumdivk")))
     777             :   {
     778        1030 :     F = clean_Z_factor(F);
     779        1030 :     v = sumdivk_aux(F,k);
     780             :   }
     781        2490 :   else if (lgefint(n) == 3)
     782             :   {
     783        2450 :     if (n[2] == 1) return gen_1;
     784        2380 :     F = factoru(n[2]);
     785        2380 :     v = usumdivk_fact(F,k);
     786             :   }
     787             :   else
     788          40 :     v = sumdivk_aux(absZ_factor(n), k);
     789        3450 :   if (k1 > 0) return gerepileuptoint(av, v);
     790           5 :   return gerepileupto(av, gdiv(v, powiu(n,k)));
     791             : }
     792             : 
     793             : GEN
     794         580 : usumdiv_fact(GEN f)
     795             : {
     796         580 :   GEN P = gel(f,1), E = gel(f,2);
     797         580 :   long i, l = lg(P);
     798         580 :   GEN v = cgetg(l, t_VEC);
     799         580 :   for (i=1; i<l; i++) gel(v,i) = u_euler_sumdiv(P[i],E[i]);
     800         580 :   return ZV_prod(v);
     801             : }
     802             : GEN
     803        2760 : usumdivk_fact(GEN f, ulong k)
     804             : {
     805        2760 :   GEN P = gel(f,1), E = gel(f,2);
     806        2760 :   long i, l = lg(P);
     807        2760 :   GEN v = cgetg(l, t_VEC);
     808        2760 :   for (i=1; i<l; i++) gel(v,i) = euler_sumdiv(powuu(P[i],k),E[i]);
     809        2760 :   return ZV_prod(v);
     810             : }
     811             : 
     812             : long
     813         225 : uissquarefree_fact(GEN f)
     814             : {
     815         225 :   GEN E = gel(f,2);
     816         225 :   long i, l = lg(E);
     817         475 :   for (i = 1; i < l; i++)
     818         355 :     if (E[i] > 1) return 0;
     819         120 :   return 1;
     820             : }
     821             : long
     822        8745 : uissquarefree(ulong n)
     823             : {
     824        8745 :   if (!n) return 0;
     825        8745 :   return moebiusu(n)? 1: 0;
     826             : }
     827             : long
     828          15 : Z_issquarefree(GEN n)
     829             : {
     830          15 :   switch(lgefint(n))
     831             :   {
     832           5 :     case 2: return 0;
     833           5 :     case 3: return uissquarefree(n[2]);
     834             :   }
     835           5 :   return moebius(n)? 1: 0;
     836             : }
     837             : long
     838         675 : issquarefree(GEN x)
     839             : {
     840             :   pari_sp av;
     841             :   GEN d;
     842         675 :   switch(typ(x))
     843             :   {
     844          10 :     case t_INT: return Z_issquarefree(x);
     845             :     case t_POL:
     846         665 :       if (!signe(x)) return 0;
     847         665 :       av = avma; d = RgX_gcd(x, RgX_deriv(x));
     848         665 :       avma = av; return (lg(d) == 3);
     849           0 :     default: pari_err_TYPE("issquarefree",x);
     850             :       return 0; /* LCOV_EXCL_LINE */
     851             :   }
     852             : }
     853             : 
     854             : /*********************************************************************/
     855             : /**                                                                 **/
     856             : /**                    DIGITS / SUM OF DIGITS                       **/
     857             : /**                                                                 **/
     858             : /*********************************************************************/
     859             : 
     860             : /* set v[i] = 1 iff B^i is needed in the digits_dac algorithm */
     861             : static void
     862     1432980 : set_vexp(GEN v, long l)
     863             : {
     864             :   long m;
     865     2865960 :   if (v[l]) return;
     866      469040 :   v[l] = 1; m = l>>1;
     867      469040 :   set_vexp(v, m);
     868      469040 :   set_vexp(v, l-m);
     869             : }
     870             : 
     871             : /* return all needed B^i for DAC algorithm, for lz digits */
     872             : static GEN
     873      494900 : get_vB(GEN T, long lz, void *E, struct bb_ring *r)
     874             : {
     875      494900 :   GEN vB, vexp = const_vecsmall(lz, 0);
     876      494900 :   long i, l = (lz+1) >> 1;
     877      494900 :   vexp[1] = 1;
     878      494900 :   vexp[2] = 1;
     879      494900 :   set_vexp(vexp, lz);
     880      494900 :   vB = zerovec(lz); /* unneeded entries remain = 0 */
     881      494900 :   gel(vB, 1) = T;
     882      984880 :   for (i = 2; i <= l; i++)
     883      489980 :     if (vexp[i])
     884             :     {
     885      469040 :       long j = i >> 1;
     886      469040 :       GEN B2j = r->sqr(E, gel(vB,j));
     887      469040 :       gel(vB,i) = odd(i)? r->mul(E, B2j, T): B2j;
     888             :     }
     889      494900 :   return vB;
     890             : }
     891             : 
     892             : static void
     893      118770 : gen_digits_dac(GEN x, GEN vB, long l, GEN *z,
     894             :                void *E, GEN div(void *E, GEN a, GEN b, GEN *r))
     895             : {
     896             :   GEN q, r;
     897      118770 :   long m = l>>1;
     898      237540 :   if (l==1) { *z=x; return; }
     899       57175 :   q = div(E, x, gel(vB,m), &r);
     900       57175 :   gen_digits_dac(r, vB, m, z, E, div);
     901       57175 :   gen_digits_dac(q, vB, l-m, z+m, E, div);
     902             : }
     903             : 
     904             : static GEN
     905       20635 : gen_fromdigits_dac(GEN x, GEN vB, long i, long l, void *E,
     906             :                    GEN add(void *E, GEN a, GEN b),
     907             :                    GEN mul(void *E, GEN a, GEN b))
     908             : {
     909             :   GEN a, b;
     910       20635 :   long m = l>>1;
     911       20635 :   if (l==1) return gel(x,i);
     912        9415 :   a = gen_fromdigits_dac(x, vB, i, m, E, add, mul);
     913        9415 :   b = gen_fromdigits_dac(x, vB, i+m, l-m, E, add, mul);
     914        9415 :   return add(E, a, mul(E, b, gel(vB, m)));
     915             : }
     916             : 
     917             : static GEN
     918        4485 : gen_digits_i(GEN x, GEN B, long n, void *E, struct bb_ring *r,
     919             :                           GEN (*div)(void *E, GEN x, GEN y, GEN *r))
     920             : {
     921             :   GEN z, vB;
     922        4485 :   if (n==1) retmkvec(gcopy(x));
     923        4420 :   vB = get_vB(B, n, E, r);
     924        4420 :   z = cgetg(n+1, t_VEC);
     925        4420 :   gen_digits_dac(x, vB, n, (GEN*)(z+1), E, div);
     926        4420 :   return z;
     927             : }
     928             : 
     929             : GEN
     930        4450 : gen_digits(GEN x, GEN B, long n, void *E, struct bb_ring *r,
     931             :                           GEN (*div)(void *E, GEN x, GEN y, GEN *r))
     932             : {
     933        4450 :   pari_sp av = avma;
     934        4450 :   return gerepilecopy(av, gen_digits_i(x, B, n, E, r, div));
     935             : }
     936             : 
     937             : GEN
     938        1805 : gen_fromdigits(GEN x, GEN B, void *E, struct bb_ring *r)
     939             : {
     940        1805 :   pari_sp av = avma;
     941        1805 :   long n = lg(x)-1;
     942        1805 :   GEN vB = get_vB(B, n, E, r);
     943        1805 :   GEN z = gen_fromdigits_dac(x, vB, 1, n, E, r->add, r->mul);
     944        1805 :   return gerepilecopy(av, z);
     945             : }
     946             : 
     947             : static GEN
     948        1265 : _addii(void *data /* ignored */, GEN x, GEN y)
     949        1265 : { (void)data; return addii(x,y); }
     950             : static GEN
     951      450435 : _sqri(void *data /* ignored */, GEN x) { (void)data; return sqri(x); }
     952             : static GEN
     953       98375 : _mulii(void *data /* ignored */, GEN x, GEN y)
     954       98375 :  { (void)data; return mulii(x,y); }
     955             : static GEN
     956         310 : _dvmdii(void *data /* ignored */, GEN x, GEN y, GEN *r)
     957         310 :  { (void)data; return dvmdii(x,y,r); }
     958             : 
     959             : static struct bb_ring Z_ring = { _addii, _mulii, _sqri };
     960             : 
     961             : static GEN
     962         130 : check_basis(GEN B)
     963             : {
     964         130 :   if (!B) return utoipos(10);
     965         115 :   if (typ(B)!=t_INT) pari_err_TYPE("digits",B);
     966         115 :   if (abscmpiu(B,2)<0) pari_err_DOMAIN("digits","B","<",gen_2,B);
     967         115 :   return B;
     968             : }
     969             : 
     970             : /* x has l digits in base B, write them to z[0..l-1], vB[i] = B^i */
     971             : static void
     972        2200 : digits_dacsmall(GEN x, GEN vB, long l, ulong* z)
     973             : {
     974        2200 :   pari_sp av = avma;
     975             :   GEN q,r;
     976             :   long m;
     977        4400 :   if (l==1) { *z=itou(x); return; }
     978        1090 :   m=l>>1;
     979        1090 :   q = dvmdii(x, gel(vB,m), &r);
     980        1090 :   digits_dacsmall(q,vB,l-m,z);
     981        1090 :   digits_dacsmall(r,vB,m,z+l-m);
     982        1090 :   avma = av;
     983             : }
     984             : 
     985             : GEN
     986          40 : digits(GEN x, GEN B)
     987             : {
     988          40 :   pari_sp av=avma;
     989             :   long lz;
     990             :   GEN z, vB;
     991          40 :   if (typ(x)!=t_INT) pari_err_TYPE("digits",x);
     992          40 :   B = check_basis(B);
     993          40 :   if (signe(B)<0) pari_err_DOMAIN("digits","B","<",gen_0,B);
     994          40 :   if (!signe(x))       {avma = av; return cgetg(1,t_VEC); }
     995          35 :   if (abscmpii(x,B)<0) {avma = av; retmkvec(absi(x)); }
     996          35 :   if (Z_ispow2(B))
     997             :   {
     998          10 :     long k = expi(B);
     999          10 :     if (k == 1) return binaire(x);
    1000           5 :     if (k < BITS_IN_LONG)
    1001             :     {
    1002           0 :       (void)new_chunk(4*(expi(x) + 2)); /* HACK */
    1003           0 :       z = binary_2k_nv(x, k);
    1004           0 :       avma = av; return Flv_to_ZV(z);
    1005             :     }
    1006             :     else
    1007             :     {
    1008           5 :       avma = av; return binary_2k(x, k);
    1009             :     }
    1010             :   }
    1011          25 :   if (signe(x) < 0) x = absi(x);
    1012          25 :   lz = logint(x,B) + 1;
    1013          25 :   if (lgefint(B)>3)
    1014             :   {
    1015           5 :     z = gerepileupto(av, gen_digits_i(x, B, lz, NULL, &Z_ring, _dvmdii));
    1016           5 :     vecreverse_inplace(z);
    1017           5 :     return z;
    1018             :   }
    1019             :   else
    1020             :   {
    1021          20 :     vB = get_vB(B, lz, NULL, &Z_ring);
    1022          20 :     (void)new_chunk(3*lz); /* HACK */
    1023          20 :     z = zero_zv(lz);
    1024          20 :     digits_dacsmall(x,vB,lz,(ulong*)(z+1));
    1025          20 :     avma = av; return Flv_to_ZV(z);
    1026             :   }
    1027             : }
    1028             : 
    1029             : static GEN
    1030     1451475 : fromdigitsu_dac(GEN x, GEN vB, long i, long l)
    1031             : {
    1032             :   GEN a, b;
    1033     1451475 :   long m = l>>1;
    1034     1451475 :   if (l==1) return utoi(uel(x,i));
    1035     1195120 :   if (l==2) return addumului(uel(x,i), uel(x,i+1), gel(vB, m));
    1036      481410 :   a = fromdigitsu_dac(x, vB, i, m);
    1037      481410 :   b = fromdigitsu_dac(x, vB, i+m, l-m);
    1038      481410 :   return addii(a, mulii(b, gel(vB, m)));
    1039             : }
    1040             : 
    1041             : GEN
    1042      488655 : fromdigitsu(GEN x, GEN B)
    1043             : {
    1044      488655 :   pari_sp av = avma;
    1045      488655 :   long n = lg(x)-1;
    1046             :   GEN vB, z;
    1047      488655 :   if (n==0) return gen_0;
    1048      488655 :   vB = get_vB(B, n, NULL, &Z_ring);
    1049      488655 :   z = fromdigitsu_dac(x, vB, 1, n);
    1050      488655 :   return gerepileuptoint(av, z);
    1051             : }
    1052             : 
    1053             : static int
    1054          10 : ZV_in_range(GEN v, GEN B)
    1055             : {
    1056          10 :   long i, l = lg(v);
    1057        1185 :   for(i=1; i < l; i++)
    1058             :   {
    1059        1180 :     GEN vi = gel(v, i);
    1060        1180 :     if (signe(vi) < 0 || cmpii(vi, B) >= 0)
    1061           5 :       return 0;
    1062             :   }
    1063           5 :   return 1;
    1064             : }
    1065             : 
    1066             : GEN
    1067          40 : fromdigits(GEN x, GEN B)
    1068             : {
    1069          40 :   pari_sp av = avma;
    1070          40 :   if (typ(x)!=t_VEC || !RgV_is_ZV(x)) pari_err_TYPE("fromdigits",x);
    1071          40 :   if (lg(x)==1) return gen_0;
    1072          35 :   B = check_basis(B);
    1073          35 :   if (Z_ispow2(B) && ZV_in_range(x, B))
    1074           5 :     return fromdigits_2k(x, expi(B));
    1075          30 :   x = vecreverse(x);
    1076          30 :   return gerepileuptoint(av, gen_fromdigits(x, B, NULL, &Z_ring));
    1077             : }
    1078             : 
    1079             : static const ulong digsum[] ={
    1080             :   0,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,10,2,3,4,5,6,7,8,9,10,11,3,4,5,6,7,8,
    1081             :   9,10,11,12,4,5,6,7,8,9,10,11,12,13,5,6,7,8,9,10,11,12,13,14,6,7,8,9,10,11,
    1082             :   12,13,14,15,7,8,9,10,11,12,13,14,15,16,8,9,10,11,12,13,14,15,16,17,9,10,11,
    1083             :   12,13,14,15,16,17,18,1,2,3,4,5,6,7,8,9,10,2,3,4,5,6,7,8,9,10,11,3,4,5,6,7,8,
    1084             :   9,10,11,12,4,5,6,7,8,9,10,11,12,13,5,6,7,8,9,10,11,12,13,14,6,7,8,9,10,11,
    1085             :   12,13,14,15,7,8,9,10,11,12,13,14,15,16,8,9,10,11,12,13,14,15,16,17,9,10,11,
    1086             :   12,13,14,15,16,17,18,10,11,12,13,14,15,16,17,18,19,2,3,4,5,6,7,8,9,10,11,3,
    1087             :   4,5,6,7,8,9,10,11,12,4,5,6,7,8,9,10,11,12,13,5,6,7,8,9,10,11,12,13,14,6,7,8,
    1088             :   9,10,11,12,13,14,15,7,8,9,10,11,12,13,14,15,16,8,9,10,11,12,13,14,15,16,17,
    1089             :   9,10,11,12,13,14,15,16,17,18,10,11,12,13,14,15,16,17,18,19,11,12,13,14,15,
    1090             :   16,17,18,19,20,3,4,5,6,7,8,9,10,11,12,4,5,6,7,8,9,10,11,12,13,5,6,7,8,9,10,
    1091             :   11,12,13,14,6,7,8,9,10,11,12,13,14,15,7,8,9,10,11,12,13,14,15,16,8,9,10,11,
    1092             :   12,13,14,15,16,17,9,10,11,12,13,14,15,16,17,18,10,11,12,13,14,15,16,17,18,
    1093             :   19,11,12,13,14,15,16,17,18,19,20,12,13,14,15,16,17,18,19,20,21,4,5,6,7,8,9,
    1094             :   10,11,12,13,5,6,7,8,9,10,11,12,13,14,6,7,8,9,10,11,12,13,14,15,7,8,9,10,11,
    1095             :   12,13,14,15,16,8,9,10,11,12,13,14,15,16,17,9,10,11,12,13,14,15,16,17,18,10,
    1096             :   11,12,13,14,15,16,17,18,19,11,12,13,14,15,16,17,18,19,20,12,13,14,15,16,17,
    1097             :   18,19,20,21,13,14,15,16,17,18,19,20,21,22,5,6,7,8,9,10,11,12,13,14,6,7,8,9,
    1098             :   10,11,12,13,14,15,7,8,9,10,11,12,13,14,15,16,8,9,10,11,12,13,14,15,16,17,9,
    1099             :   10,11,12,13,14,15,16,17,18,10,11,12,13,14,15,16,17,18,19,11,12,13,14,15,16,
    1100             :   17,18,19,20,12,13,14,15,16,17,18,19,20,21,13,14,15,16,17,18,19,20,21,22,14,
    1101             :   15,16,17,18,19,20,21,22,23,6,7,8,9,10,11,12,13,14,15,7,8,9,10,11,12,13,14,
    1102             :   15,16,8,9,10,11,12,13,14,15,16,17,9,10,11,12,13,14,15,16,17,18,10,11,12,13,
    1103             :   14,15,16,17,18,19,11,12,13,14,15,16,17,18,19,20,12,13,14,15,16,17,18,19,20,
    1104             :   21,13,14,15,16,17,18,19,20,21,22,14,15,16,17,18,19,20,21,22,23,15,16,17,18,
    1105             :   19,20,21,22,23,24,7,8,9,10,11,12,13,14,15,16,8,9,10,11,12,13,14,15,16,17,9,
    1106             :   10,11,12,13,14,15,16,17,18,10,11,12,13,14,15,16,17,18,19,11,12,13,14,15,16,
    1107             :   17,18,19,20,12,13,14,15,16,17,18,19,20,21,13,14,15,16,17,18,19,20,21,22,14,
    1108             :   15,16,17,18,19,20,21,22,23,15,16,17,18,19,20,21,22,23,24,16,17,18,19,20,21,
    1109             :   22,23,24,25,8,9,10,11,12,13,14,15,16,17,9,10,11,12,13,14,15,16,17,18,10,11,
    1110             :   12,13,14,15,16,17,18,19,11,12,13,14,15,16,17,18,19,20,12,13,14,15,16,17,18,
    1111             :   19,20,21,13,14,15,16,17,18,19,20,21,22,14,15,16,17,18,19,20,21,22,23,15,16,
    1112             :   17,18,19,20,21,22,23,24,16,17,18,19,20,21,22,23,24,25,17,18,19,20,21,22,23,
    1113             :   24,25,26,9,10,11,12,13,14,15,16,17,18,10,11,12,13,14,15,16,17,18,19,11,12,
    1114             :   13,14,15,16,17,18,19,20,12,13,14,15,16,17,18,19,20,21,13,14,15,16,17,18,19,
    1115             :   20,21,22,14,15,16,17,18,19,20,21,22,23,15,16,17,18,19,20,21,22,23,24,16,17,
    1116             :   18,19,20,21,22,23,24,25,17,18,19,20,21,22,23,24,25,26,18,19,20,21,22,23,24,
    1117             :   25,26,27
    1118             : };
    1119             : 
    1120             : ulong
    1121      253680 : sumdigitsu(ulong n)
    1122             : {
    1123      253680 :   ulong s = 0;
    1124      253680 :   while (n) { s += digsum[n % 1000]; n /= 1000; }
    1125      253680 :   return s;
    1126             : }
    1127             : 
    1128             : /* res=array of 9-digits integers, return \sum_{0 <= i < l} sumdigits(res[i]) */
    1129             : static ulong
    1130          10 : sumdigits_block(ulong *res, long l)
    1131             : {
    1132          10 :   long s = sumdigitsu(*--res);
    1133          10 :   while (--l > 0) s += sumdigitsu(*--res);
    1134          10 :   return s;
    1135             : }
    1136             : 
    1137             : GEN
    1138          25 : sumdigits(GEN n)
    1139             : {
    1140          25 :   pari_sp av = avma;
    1141             :   ulong s, *res;
    1142             :   long l;
    1143             : 
    1144          25 :   if (typ(n) != t_INT) pari_err_TYPE("sumdigits", n);
    1145          25 :   l = lgefint(n);
    1146          25 :   switch(l)
    1147             :   {
    1148           5 :     case 2: return gen_0;
    1149          10 :     case 3: return utoipos(sumdigitsu(n[2]));
    1150             :   }
    1151          10 :   res = convi(n, &l);
    1152          10 :   if ((ulong)l < ULONG_MAX / 81)
    1153             :   {
    1154          10 :     s = sumdigits_block(res, l);
    1155          10 :     avma = av; return utoipos(s);
    1156             :   }
    1157             :   else /* Huge. Overflows ulong */
    1158             :   {
    1159           0 :     const long L = (long)(ULONG_MAX / 81);
    1160           0 :     GEN S = gen_0;
    1161           0 :     while (l > L)
    1162             :     {
    1163           0 :       S = addiu(S, sumdigits_block(res, L));
    1164           0 :       res += L; l -= L;
    1165             :     }
    1166           0 :     if (l)
    1167           0 :       S = addiu(S, sumdigits_block(res, l));
    1168           0 :     return gerepileuptoint(av, S);
    1169             :   }
    1170             : }
    1171             : 
    1172             : GEN
    1173          75 : sumdigits0(GEN x, GEN B)
    1174             : {
    1175          75 :   pari_sp av = avma;
    1176             :   GEN z;
    1177             :   long lz;
    1178             : 
    1179          75 :   if (!B) return sumdigits(x);
    1180          55 :   if (typ(x) != t_INT) pari_err_TYPE("sumdigits", x);
    1181          55 :   B = check_basis(B);
    1182          55 :   if (Z_ispow2(B))
    1183             :   {
    1184          20 :     long k = expi(B);
    1185          20 :     if (k == 1) { avma = av; return utoi(hammingweight(x)); }
    1186          15 :     if (k < BITS_IN_LONG)
    1187             :     {
    1188          10 :       GEN z = binary_2k_nv(x, k);
    1189          10 :       if (lg(z)-1 > 1L<<(BITS_IN_LONG-k)) /* may overflow */
    1190           0 :         return gerepileuptoint(av, ZV_sum(Flv_to_ZV(z)));
    1191          10 :       avma = av; return utoi(zv_sum(z));
    1192             :     }
    1193           5 :     return gerepileuptoint(av, ZV_sum(binary_2k(x, k)));
    1194             :   }
    1195          35 :   if (!signe(x))       { avma = av; return gen_0; }
    1196          35 :   if (abscmpii(x,B)<0) { avma = av; return absi(x); }
    1197          35 :   if (absequaliu(B,10))   { avma = av; return sumdigits(x); }
    1198          30 :   lz = logint(x,B) + 1;
    1199          30 :   z = gen_digits_i(x, B, lz, NULL, &Z_ring, _dvmdii);
    1200          30 :   return gerepileuptoint(av, ZV_sum(z));
    1201             : }

Generated by: LCOV version 1.11