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 19834-0e97742) Lines: 554 600 92.3 %
Date: 2016-12-09 05:49:11 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          35 : boundfact(GEN n, ulong lim)
      25             : {
      26          35 :   switch(typ(n))
      27             :   {
      28          21 :     case t_INT: return Z_factor_limit(n,lim);
      29             :     case t_FRAC: {
      30          14 :       pari_sp av = avma;
      31          14 :       GEN a = Z_factor_limit(gel(n,1),lim);
      32          14 :       GEN b = Z_factor_limit(gel(n,2),lim);
      33          14 :       gel(b,2) = ZC_neg(gel(b,2));
      34          14 :       return gerepilecopy(av, merge_factor(a,b,(void*)&cmpii,cmp_nodata));
      35             :     }
      36             :   }
      37           0 :   pari_err_TYPE("boundfact",n);
      38           0 :   return NULL; /* not reached */
      39             : }
      40             : 
      41             : /* NOT memory clean */
      42             : GEN
      43       10429 : Z_smoothen(GEN N, GEN L, GEN *pP, GEN *pe)
      44             : {
      45       10429 :   long i, j, l = lg(L);
      46       10429 :   GEN e = new_chunk(l), P = new_chunk(l);
      47       96056 :   for (i = j = 1; i < l; i++)
      48             :   {
      49       91657 :     ulong p = uel(L,i);
      50       91657 :     long v = Z_lvalrem(N, p, &N);
      51       91657 :     if (v) { P[j] = p; e[j] = v; j++; if (is_pm1(N)) { N = NULL; break; } }
      52             :   }
      53       10429 :   P[0] = evaltyp(t_VECSMALL) | evallg(j); *pP = P;
      54       10429 :   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       27816 : factoru_pow(ulong n)
      65             : {
      66       27816 :   GEN f = cgetg(4,t_VEC);
      67       27814 :   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       27814 :   (void)new_chunk((15 + 1)*3);
      72       27814 :   F = factoru(n);
      73       27817 :   P = gel(F,1);
      74       27817 :   E = gel(F,2); l = lg(P);
      75       27817 :   avma = av;
      76       27817 :   gel(f,1) = p = cgetg(l,t_VECSMALL);
      77       27818 :   gel(f,2) = e = cgetg(l,t_VECSMALL);
      78       27817 :   gel(f,3) = c = cgetg(l,t_VECSMALL);
      79       88713 :   for(i = 1; i < l; i++)
      80             :   {
      81       60895 :     p[i] = P[i];
      82       60895 :     e[i] = E[i];
      83       60895 :     c[i] = upowuu(p[i], e[i]);
      84             :   }
      85       27818 :   return f;
      86             : }
      87             : 
      88             : static GEN
      89       80216 : factorlim(GEN n, ulong lim)
      90       80216 : { 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       62226 : factor_pn_1_limit(GEN p, long n, ulong lim)
      95             : {
      96       62226 :   pari_sp av = avma;
      97       62226 :   GEN A = factorlim(subiu(p,1), lim), d = divisorsu(n);
      98       62226 :   long i, pp = itos_or_0(p);
      99       75022 :   for(i=2; i<lg(d); i++)
     100             :   {
     101             :     GEN B;
     102       23660 :     if (pp && d[i]%pp==0 && (
     103       21728 :        ((pp&3)==1 && (d[i]&1)) ||
     104       10955 :        ((pp&3)==3 && (d[i]&3)==2) ||
     105       10759 :        (pp==2 && (d[i]&7)==4)))
     106        5194 :     {
     107        5194 :       GEN f=factor_Aurifeuille_prime(p,d[i]);
     108        5194 :       B = factorlim(f, lim);
     109        5194 :       A = merge_factor(A, B, (void*)&cmpii, cmp_nodata);
     110        5194 :       B = factorlim(diviiexact(polcyclo_eval(d[i],p), f), lim);
     111             :     }
     112             :     else
     113        7602 :       B = factorlim(polcyclo_eval(d[i],p), lim);
     114       12796 :     A = merge_factor(A, B, (void*)&cmpii, cmp_nodata);
     115             :   }
     116       62226 :   return gerepilecopy(av, A);
     117             : }
     118             : GEN
     119       62226 : factor_pn_1(GEN p, ulong n)
     120       62226 : { 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 = subis(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     3124747 : RgV_is_ZVpos(GEN v)
     186             : {
     187     3124747 :   long i, l = lg(v);
     188     9627502 :   for (i = 1; i < l; i++)
     189             :   {
     190     6502762 :     GEN c = gel(v,i);
     191     6502762 :     if (typ(c) != t_INT || signe(c) <= 0) return 0;
     192             :   }
     193     3124740 :   return 1;
     194             : }
     195             : /* check whether v is a ZV with non-0 entries */
     196             : static int
     197        5166 : RgV_is_ZVnon0(GEN v)
     198             : {
     199        5166 :   long i, l = lg(v);
     200       15785 :   for (i = 1; i < l; i++)
     201             :   {
     202       10668 :     GEN c = gel(v,i);
     203       10668 :     if (typ(c) != t_INT || !signe(c)) return 0;
     204             :   }
     205        5117 :   return 1;
     206             : }
     207             : /* check whether v is a ZV with non-zero entries OR exactly [0] */
     208             : static int
     209        3178 : RgV_is_ZV0(GEN v)
     210             : {
     211        3178 :   long i, l = lg(v);
     212        9422 :   for (i = 1; i < l; i++)
     213             :   {
     214        6335 :     GEN c = gel(v,i);
     215             :     long s;
     216        6335 :     if (typ(c) != t_INT) return 0;
     217        6335 :     s = signe(c);
     218        6335 :     if (!s) return (l == 2);
     219             :   }
     220        3087 :   return 1;
     221             : }
     222             : 
     223             : static int
     224        2646 : RgV_is_prV(GEN v)
     225             : {
     226        2646 :   long l = lg(v), i;
     227        2996 :   for (i = 1; i < l; i++)
     228        2653 :     if (!checkprid_i(gel(v,i))) return 0;
     229         343 :   return 1;
     230             : }
     231             : int
     232        6328 : is_nf_factor(GEN F)
     233             : {
     234       17759 :   return typ(F) == t_MAT && lg(F) == 3
     235        8967 :     && RgV_is_prV(gel(F,1)) && RgV_is_ZVpos(gel(F,2));
     236             : }
     237             : int
     238          14 : is_nf_extfactor(GEN F)
     239             : {
     240          42 :   return typ(F) == t_MAT && lg(F) == 3
     241          21 :     && RgV_is_prV(gel(F,1)) && RgV_is_ZV(gel(F,2));
     242             : }
     243             : 
     244             : static int
     245     1566388 : is_Z_factor_i(GEN f)
     246     1566388 : { return typ(f) == t_MAT && lg(f) == 3 && RgV_is_ZVpos(gel(f,2)); }
     247             : int
     248     1558044 : is_Z_factorpos(GEN f)
     249     1558044 : { return is_Z_factor_i(f) && RgV_is_ZVpos(gel(f,1)); }
     250             : int
     251        3178 : is_Z_factor(GEN f)
     252        3178 : { return is_Z_factor_i(f) && RgV_is_ZV0(gel(f,1)); }
     253             : /* as is_Z_factorpos, also allow factor(0) */
     254             : int
     255        5166 : is_Z_factornon0(GEN f)
     256        5166 : { return is_Z_factor_i(f) && RgV_is_ZVnon0(gel(f,1)); }
     257             : GEN
     258        4501 : clean_Z_factor(GEN f)
     259             : {
     260        4501 :   GEN P = gel(f,1);
     261        4501 :   long n = lg(P)-1;
     262        4501 :   if (n && equalim1(gel(P,1)))
     263        2156 :     return mkmat2(vecslice(P,2,n), vecslice(gel(f,2),2,n));
     264        2345 :   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         189 : check_arith_pos(GEN n, const char *f) {
     287         189 :   switch(typ(n))
     288             :   {
     289             :     case t_INT:
     290         189 :       if (signe(n) <= 0) pari_err_DOMAIN(f, "argument", "<=", gen_0, gen_0);
     291         189 :       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       33512 : check_arith_non0(GEN n, const char *f) {
     305       33512 :   switch(typ(n))
     306             :   {
     307             :     case t_INT:
     308       28707 :       if (!signe(n)) pari_err_DOMAIN(f, "argument", "=", gen_0, gen_0);
     309       28677 :       return NULL;
     310             :     case t_VEC:
     311           7 :       if (lg(n) != 3 || typ(gel(n,1)) != t_INT || !signe(gel(n,1))) break;
     312           7 :       n = gel(n,2); /* fall through */
     313             :     case t_MAT:
     314        5117 :       if (!is_Z_factornon0(n)) break;
     315        5068 :       return n;
     316             :   }
     317          42 :   pari_err_TYPE(f,n);
     318           0 :   return NULL;
     319             : }
     320             : /* n attached to a factorization of an integer */
     321             : GEN
     322      187656 : check_arith_all(GEN n, const char *f) {
     323      187656 :   switch(typ(n))
     324             :   {
     325             :     case t_INT:
     326      184478 :       return NULL;
     327             :     case t_VEC:
     328         126 :       if (lg(n) != 3 || typ(gel(n,1)) != t_INT) break;
     329         126 :       n = gel(n,2); /* fall through */
     330             :     case t_MAT:
     331        3178 :       if (!is_Z_factor(n)) break;
     332        3178 :       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      350749 : set_fact_check(GEN F, GEN *pP, GEN *pE, int *isint)
     347             : {
     348             :   GEN E, P;
     349      350749 :   if (lg(F) != 3) pari_err_TYPE("divisors",F);
     350      350749 :   P = gel(F,1);
     351      350749 :   E = gel(F,2);
     352      350749 :   RgV_check_ZV(E, "divisors");
     353      350749 :   *isint = RgV_is_ZV(P);
     354      350749 :   if (*isint)
     355             :   {
     356      350735 :     long i, l = lg(P);
     357             :     /* skip -1 */
     358      350735 :     if (l>1 && signe(gel(P,1)) < 0) { E++; P = vecslice(P,2,--l); }
     359             :     /* test for 0 */
     360     1143135 :     for (i = 1; i < l; i++)
     361      792407 :       if (!signe(gel(P,i)) && signe(gel(E,i)))
     362           7 :         pari_err_DOMAIN("divisors", "argument", "=", gen_0, F);
     363             :   }
     364      350742 :   *pP = P;
     365      350742 :   *pE = E;
     366      350742 : }
     367             : static void
     368       10640 : set_fact(GEN F, GEN *pP, GEN *pE) { *pP = gel(F,1); *pE = gel(F,2); }
     369             : 
     370             : int
     371      361396 : divisors_init(GEN n, GEN *pP, GEN *pE)
     372             : {
     373             :   long i,l;
     374             :   GEN E, P, e;
     375             :   int isint;
     376             : 
     377      361396 :   switch(typ(n))
     378             :   {
     379             :     case t_INT:
     380       10619 :       if (!signe(n)) pari_err_DOMAIN("divisors", "argument", "=", gen_0, gen_0);
     381       10619 :       set_fact(absZ_factor(n), &P,&E);
     382       10619 :       isint = 1; break;
     383             :     case t_VEC:
     384          14 :       if (lg(n) != 3 || typ(gel(n,2)) !=t_MAT) pari_err_TYPE("divisors",n);
     385           7 :       set_fact_check(gel(n,2), &P,&E, &isint);
     386           7 :       break;
     387             :     case t_MAT:
     388      350742 :       set_fact_check(n, &P,&E, &isint);
     389      350735 :       break;
     390             :     default:
     391          21 :       set_fact(factor(n), &P,&E);
     392          21 :       isint = 0; break;
     393             :   }
     394      361382 :   l = lg(P);
     395      361382 :   e = cgetg(l, t_VECSMALL);
     396     1184323 :   for (i=1; i<l; i++)
     397             :   {
     398      822948 :     e[i] = itos(gel(E,i));
     399      822948 :     if (e[i] < 0) pari_err_TYPE("divisors [denominator]",n);
     400             :   }
     401      361375 :   *pP = P; *pE = e; return isint;
     402             : }
     403             : 
     404             : GEN
     405      361354 : divisors(GEN n)
     406             : {
     407      361354 :   pari_sp av = avma;
     408             :   long i, j, l;
     409             :   ulong ndiv;
     410             :   GEN *d, *t, *t1, *t2, *t3, P, E, e;
     411      361354 :   int isint = divisors_init(n, &P, &E);
     412             : 
     413      361333 :   l = lg(E); e = cgetg(l, t_VECSMALL);
     414      361333 :   for (i=1; i<l; i++) e[i] = E[i]+1;
     415      361333 :   ndiv = itou_or_0( zv_prod_Z(e) );
     416      361333 :   if (!ndiv || ndiv & ~LGBITS) pari_err_OVERFLOW("divisors");
     417      361333 :   d = t = (GEN*) cgetg(ndiv+1,t_VEC);
     418      361333 :   *++d = gen_1;
     419      361333 :   if (isint)
     420             :   {
     421     1184064 :     for (i=1; i<l; i++)
     422     2210012 :       for (t1=t,j=E[i]; j; j--,t1=t2)
     423     1387267 :         for (t2=d, t3=t1; t3<t2; ) *++d = mulii(*++t3, gel(P,i));
     424      361319 :     e = ZV_sort((GEN)t);
     425             :   } else {
     426          56 :     for (i=1; i<l; i++)
     427         182 :       for (t1=t,j=E[i]; j; j--,t1=t2)
     428         140 :         for (t2=d, t3=t1; t3<t2; ) *++d = gmul(*++t3, gel(P,i));
     429          14 :     e = (GEN)t;
     430             :   }
     431      361333 :   return gerepileupto(av, e);
     432             : }
     433             : 
     434             : GEN
     435       91243 : divisorsu_fact(GEN P, GEN E)
     436             : {
     437       91243 :   long i, j, l = lg(P);
     438       91243 :   ulong nbdiv = 1;
     439             :   GEN d, t, t1, t2, t3;
     440       91243 :   for (i=1; i<l; i++) nbdiv *= 1+E[i];
     441       91243 :   d = t = cgetg(nbdiv+1,t_VECSMALL);
     442       91243 :   *++d = 1;
     443      135503 :   for (i=1; i<l; i++)
     444      100816 :     for (t1=t,j=E[i]; j; j--,t1=t2)
     445       56556 :       for (t2=d, t3=t1; t3<t2; ) *(++d) = *(++t3) * P[i];
     446       91243 :   vecsmall_sort(t); return t;
     447             : }
     448             : GEN
     449       90025 : divisorsu(ulong n)
     450             : {
     451       90025 :   pari_sp av = avma;
     452       90025 :   GEN fa = factoru(n);
     453       90025 :   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         763 : core2fa(GEN fa)
     467             : {
     468         763 :   GEN P = gel(fa,1), E = gel(fa,2), c = gen_1, f = gen_1;
     469             :   long i;
     470        1750 :   for (i=1; i<lg(P); i++)
     471             :   {
     472         987 :     long e = itos(gel(E,i));
     473         987 :     if (e & 1)  c = mulii(c, gel(P,i));
     474         987 :     if (e != 1) f = mulii(f, powiu(gel(P,i), e >> 1));
     475             :   }
     476         763 :   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         763 : core2partial(GEN n, long all)
     487             : {
     488         763 :   pari_sp av = avma;
     489         763 :   if (typ(n) != t_INT) pari_err_TYPE("core2partial",n);
     490         763 :   return gerepilecopy(av, core2fa(Z_factor_limit(n,all)));
     491             : }
     492             : static GEN
     493        1484 : core2_i(GEN n)
     494             : {
     495        1484 :   GEN f = core(n);
     496        1484 :   if (!signe(f)) return mkvec2(gen_0, gen_1);
     497        1449 :   switch(typ(n))
     498             :   {
     499           7 :     case t_VEC: n = gel(n,1); break;
     500         721 :     case t_MAT: n = factorback(n); break;
     501             :   }
     502        1449 :   return mkvec2(f, sqrtint(diviiexact(n, f)));
     503             : }
     504             : GEN
     505        1477 : core2(GEN n) { pari_sp av = avma; return gerepilecopy(av, core2_i(n)); }
     506             : 
     507             : GEN
     508        3094 : core0(GEN n,long flag) { return flag? core2(n): core(n); }
     509             : 
     510             : static long
     511          14 : _mod4(GEN c) {
     512          14 :   long r, s = signe(c);
     513          14 :   if (!s) return 0;
     514          14 :   r = mod4(c); if (s < 0) r = 4-r;
     515          14 :   return r;
     516             : }
     517             : 
     518             : long
     519        2780 : corediscs(long D, ulong *f)
     520             : {
     521             :   /* D = f^2 dK */
     522        2780 :   long dK = D>=0 ? (long) coreu(D) : -(long) coreu(-(ulong) D);
     523        2780 :   ulong dKmod4 = ((ulong)dK)&3UL;
     524        2780 :   if (dKmod4 == 2 || dKmod4 == 3)
     525         231 :     dK *= 4;
     526        2780 :   if (f) *f = usqrt((ulong)(D/dK));
     527        2780 :   return D;
     528             : }
     529             : 
     530             : GEN
     531           7 : coredisc(GEN n)
     532             : {
     533           7 :   pari_sp av = avma;
     534           7 :   GEN c = core(n);
     535           7 :   if (_mod4(c)<=1) return c; /* c = 0 or 1 mod 4 */
     536           7 :   return gerepileuptoint(av, shifti(c,2));
     537             : }
     538             : 
     539             : GEN
     540           7 : coredisc2(GEN n)
     541             : {
     542           7 :   pari_sp av = avma;
     543           7 :   GEN y = core2_i(n);
     544           7 :   GEN c = gel(y,1), f = gel(y,2);
     545           7 :   if (_mod4(c)<=1) return gerepilecopy(av, y);
     546           7 :   y = cgetg(3,t_VEC);
     547           7 :   gel(y,1) = shifti(c,2);
     548           7 :   gel(y,2) = gmul2n(f,-1); return gerepileupto(av, y);
     549             : }
     550             : 
     551             : GEN
     552          14 : coredisc0(GEN n,long flag) { return flag? coredisc2(n): coredisc(n); }
     553             : 
     554             : long
     555         815 : omegau(ulong n)
     556             : {
     557             :   pari_sp av;
     558             :   GEN F;
     559         815 :   if (n == 1UL) return 0;
     560         801 :   av = avma; F = factoru(n);
     561         801 :   avma = av; return lg(gel(F,1))-1;
     562             : }
     563             : long
     564        1589 : omega(GEN n)
     565             : {
     566             :   pari_sp av;
     567             :   GEN F, P;
     568        1589 :   if ((F = check_arith_non0(n,"omega"))) {
     569             :     long n;
     570         721 :     P = gel(F,1); n = lg(P)-1;
     571         721 :     if (n && equalim1(gel(P,1))) n--;
     572         721 :     return n;
     573             :   }
     574         854 :   if (lgefint(n) == 3) return omegau(n[2]);
     575          39 :   av = avma;
     576          39 :   F = absZ_factor(n);
     577          39 :   P = gel(F,1); avma = av; return lg(P)-1;
     578             : }
     579             : 
     580             : long
     581         821 : bigomegau(ulong n)
     582             : {
     583             :   pari_sp av;
     584             :   GEN F;
     585         821 :   if (n == 1) return 0;
     586         807 :   av = avma; F = factoru(n);
     587         807 :   avma = av; return zv_sum(gel(F,2));
     588             : }
     589             : long
     590        1589 : bigomega(GEN n)
     591             : {
     592        1589 :   pari_sp av = avma;
     593             :   GEN F, E;
     594        1589 :   if ((F = check_arith_non0(n,"bigomega")))
     595             :   {
     596         721 :     GEN P = gel(F,1);
     597         721 :     long n = lg(P)-1;
     598         721 :     E = gel(F,2);
     599         721 :     if (n && equalim1(gel(P,1))) E = vecslice(E,2,n);
     600             :   }
     601         854 :   else if (lgefint(n) == 3)
     602         821 :     return bigomegau(n[2]);
     603             :   else
     604          33 :     E = gel(absZ_factor(n), 2);
     605         754 :   E = ZV_to_zv(E);
     606         754 :   avma = av; return zv_sum(E);
     607             : }
     608             : 
     609             : /* assume f = factoru(n), possibly with 0 exponents. Return phi(n) */
     610             : ulong
     611      151253 : eulerphiu_fact(GEN f)
     612             : {
     613      151253 :   GEN P = gel(f,1), E = gel(f,2);
     614      151253 :   long i, m = 1, l = lg(P);
     615      595772 :   for (i = 1; i < l; i++)
     616             :   {
     617      444519 :     ulong p = P[i], e = E[i];
     618      444519 :     if (!e) continue;
     619      444519 :     if (p == 2)
     620      113950 :     { if (e > 1) m <<= e-1; }
     621             :     else
     622             :     {
     623      330569 :       m *= (p-1);
     624      330569 :       if (e > 1) m *= upowuu(p, e-1);
     625             :     }
     626             :   }
     627      151253 :   return m;
     628             : }
     629             : ulong
     630      149901 : eulerphiu(ulong n)
     631             : {
     632      149901 :   pari_sp av = avma;
     633             :   GEN F;
     634      149901 :   if (!n) return 2;
     635      149901 :   F = factoru(n);
     636      149901 :   avma = av; return eulerphiu_fact(F);
     637             : }
     638             : GEN
     639      145607 : eulerphi(GEN n)
     640             : {
     641      145607 :   pari_sp av = avma;
     642             :   GEN Q, F, P, E;
     643             :   long i, l;
     644             : 
     645      145607 :   if ((F = check_arith_all(n,"eulerphi")))
     646             :   {
     647         735 :     F = clean_Z_factor(F);
     648         735 :     n = (typ(n) == t_VEC)? gel(n,1): factorback(F);
     649         735 :     if (lgefint(n) == 3)
     650             :     {
     651             :       ulong e;
     652         721 :       F = mkmat2(ZV_to_nv(gel(F,1)), ZV_to_nv(gel(F,2)));
     653         721 :       e = eulerphiu_fact(F);
     654         721 :       avma = av; return utoipos(e);
     655             :     }
     656             :   }
     657      144872 :   else if (lgefint(n) == 3) return utoipos(eulerphiu(uel(n,2)));
     658             :   else
     659          53 :     F = absZ_factor(n);
     660          67 :   if (!signe(n)) return gen_2;
     661          39 :   P = gel(F,1);
     662          39 :   E = gel(F,2); l = lg(P);
     663          39 :   Q = cgetg(l, t_VEC);
     664         252 :   for (i = 1; i < l; i++)
     665             :   {
     666         213 :     GEN p = gel(P,i), q;
     667         213 :     ulong v = itou(gel(E,i));
     668         213 :     q = subiu(p,1);
     669         213 :     if (v != 1) q = mulii(q, v == 2? p: powiu(p, v-1));
     670         213 :     gel(Q,i) = q;
     671             :   }
     672          39 :   return gerepileuptoint(av, ZV_prod(Q));
     673             : }
     674             : 
     675             : static GEN
     676         760 : numdiv_aux(GEN F)
     677             : {
     678         760 :   GEN x, E = gel(F,2);
     679         760 :   long i, l = lg(E);
     680         760 :   x = cgetg(l, t_VECSMALL);
     681         760 :   for (i=1; i<l; i++) x[i] = itou(gel(E,i))+1;
     682         760 :   return x;
     683             : }
     684             : GEN
     685        1897 : numdiv(GEN n)
     686             : {
     687        1897 :   pari_sp av = avma;
     688             :   GEN F, E;
     689             :   long i, l;
     690        1897 :   if ((F = check_arith_non0(n,"numdiv")))
     691             :   {
     692         721 :     F = clean_Z_factor(F);
     693         721 :     E = numdiv_aux(F);
     694             :   }
     695        1162 :   else if (lgefint(n) == 3)
     696             :   {
     697        1123 :     if (n[2] == 1) return gen_1;
     698        1095 :     F = factoru(n[2]);
     699        1095 :     E = gel(F,2); l = lg(E);
     700        1095 :     for (i=1; i<l; i++) E[i]++;
     701             :   }
     702             :   else
     703          39 :     E = numdiv_aux(absZ_factor(n));
     704        1855 :   return gerepileuptoint(av, zv_prod_Z(E));
     705             : }
     706             : 
     707             : /* 1 + p + ... + p^v, p != 2^BIL - 1 */
     708             : static GEN
     709        1635 : u_euler_sumdiv(ulong p, long v)
     710             : {
     711        1635 :   GEN u = utoipos(1 + p); /* can't overflow */
     712        1635 :   for (; v > 1; v--) u = addsi(1, mului(p, u));
     713        1635 :   return u;
     714             : }
     715             : /* 1 + q + ... + q^v */
     716             : static GEN
     717        9677 : euler_sumdiv(GEN q, long v)
     718             : {
     719        9677 :   GEN u = addui(1, q);
     720        9677 :   for (; v > 1; v--) u = addui(1, mulii(q, u));
     721        9677 :   return u;
     722             : }
     723             : 
     724             : static GEN
     725         753 : sumdiv_aux(GEN F)
     726             : {
     727         753 :   GEN x, P = gel(F,1), E = gel(F,2);
     728         753 :   long i, l = lg(P);
     729         753 :   x = cgetg(l, t_VEC);
     730         753 :   for (i=1; i<l; i++) gel(x,i) = euler_sumdiv(gel(P,i), itou(gel(E,i)));
     731         753 :   return ZV_prod(x);
     732             : }
     733             : GEN
     734        1589 : sumdiv(GEN n)
     735             : {
     736        1589 :   pari_sp av = avma;
     737             :   GEN F, v;
     738             : 
     739        1589 :   if ((F = check_arith_non0(n,"sumdiv")))
     740             :   {
     741         721 :     F = clean_Z_factor(F);
     742         721 :     v = sumdiv_aux(F);
     743             :   }
     744         854 :   else if (lgefint(n) == 3)
     745             :   {
     746         822 :     if (n[2] == 1) return gen_1;
     747         808 :     F = factoru(n[2]);
     748         808 :     v = usumdiv_fact(F);
     749             :   }
     750             :   else
     751          32 :     v = sumdiv_aux(absZ_factor(n));
     752        1561 :   return gerepileuptoint(av, v);
     753             : }
     754             : 
     755             : static GEN
     756        1506 : sumdivk_aux(GEN F, long k)
     757             : {
     758        1506 :   GEN x, P = gel(F,1), E = gel(F,2);
     759        1506 :   long i, l = lg(P);
     760        1506 :   x = cgetg(l, t_VEC);
     761        1506 :   for (i=1; i<l; i++) gel(x,i) = euler_sumdiv(powiu(gel(P,i),k), gel(E,i)[2]);
     762        1506 :   return ZV_prod(x);
     763             : }
     764             : GEN
     765        6545 : sumdivk(GEN n, long k)
     766             : {
     767        6545 :   pari_sp av = avma;
     768             :   GEN F, v;
     769             :   long k1;
     770             : 
     771        6545 :   if (!k) return numdiv(n);
     772        6545 :   if (k == 1) return sumdiv(n);
     773        4956 :   if (k ==-1) return gerepileupto(av, gdiv(sumdiv(n), n));
     774        4956 :   k1 = k;
     775        4956 :   if (k < 0)  k = -k;
     776        4956 :   if ((F = check_arith_non0(n,"sumdivk")))
     777             :   {
     778        1442 :     F = clean_Z_factor(F);
     779        1442 :     v = sumdivk_aux(F,k);
     780             :   }
     781        3486 :   else if (lgefint(n) == 3)
     782             :   {
     783        3422 :     if (n[2] == 1) return gen_1;
     784        3324 :     F = factoru(n[2]);
     785        3324 :     v = usumdivk_fact(F,k);
     786             :   }
     787             :   else
     788          64 :     v = sumdivk_aux(absZ_factor(n), k);
     789        4830 :   if (k1 > 0) return gerepileuptoint(av, v);
     790           7 :   return gerepileupto(av, gdiv(v, powiu(n,k)));
     791             : }
     792             : 
     793             : GEN
     794         808 : usumdiv_fact(GEN f)
     795             : {
     796         808 :   GEN P = gel(f,1), E = gel(f,2);
     797         808 :   long i, l = lg(P);
     798         808 :   GEN v = cgetg(l, t_VEC);
     799         808 :   for (i=1; i<l; i++) gel(v,i) = u_euler_sumdiv(P[i],E[i]);
     800         808 :   return ZV_prod(v);
     801             : }
     802             : GEN
     803        3380 : usumdivk_fact(GEN f, ulong k)
     804             : {
     805        3380 :   GEN P = gel(f,1), E = gel(f,2);
     806        3380 :   long i, l = lg(P);
     807        3380 :   GEN v = cgetg(l, t_VEC);
     808        3380 :   for (i=1; i<l; i++) gel(v,i) = euler_sumdiv(powuu(P[i],k),E[i]);
     809        3380 :   return ZV_prod(v);
     810             : }
     811             : 
     812             : long
     813         287 : uissquarefree_fact(GEN f)
     814             : {
     815         287 :   GEN E = gel(f,2);
     816         287 :   long i, l = lg(E);
     817         581 :   for (i = 1; i < l; i++)
     818         441 :     if (E[i] > 1) return 0;
     819         140 :   return 1;
     820             : }
     821             : long
     822       11513 : uissquarefree(ulong n)
     823             : {
     824       11513 :   if (!n) return 0;
     825       11513 :   return moebiusu(n)? 1: 0;
     826             : }
     827             : long
     828         772 : Z_issquarefree(GEN n)
     829             : {
     830         772 :   switch(lgefint(n))
     831             :   {
     832           7 :     case 2: return 0;
     833           6 :     case 3: return uissquarefree(n[2]);
     834             :   }
     835         759 :   return moebius(n)? 1: 0;
     836             : }
     837             : long
     838       46970 : issquarefree(GEN x)
     839             : {
     840             :   pari_sp av;
     841             :   GEN d;
     842       46970 :   switch(typ(x))
     843             :   {
     844          14 :     case t_INT: return Z_issquarefree(x);
     845             :     case t_POL:
     846       46956 :       if (!signe(x)) return 0;
     847       46956 :       av = avma; d = RgX_gcd(x, RgX_deriv(x));
     848       46956 :       avma = av; return (lg(d) == 3);
     849           0 :     default: pari_err_TYPE("issquarefree",x);
     850           0 :       return 0; /* not reached */
     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     2356712 : set_vexp(GEN v, long l)
     863             : {
     864             :   long m;
     865     4713424 :   if (v[l]) return;
     866      798042 :   v[l] = 1; m = l>>1;
     867      798042 :   set_vexp(v, m);
     868      798042 :   set_vexp(v, l-m);
     869             : }
     870             : 
     871             : /* return all needed B^i for DAC algorithm, for lz digits */
     872             : static GEN
     873      760628 : get_vB(GEN T, long lz, void *E, struct bb_ring *r)
     874             : {
     875      760628 :   GEN vB, vexp = const_vecsmall(lz, 0);
     876      760628 :   long i, l = (lz+1) >> 1;
     877      760628 :   vexp[1] = 1;
     878      760628 :   vexp[2] = 1;
     879      760628 :   set_vexp(vexp, lz);
     880      760628 :   vB = zerovec(lz); /* unneeded entries remain = 0 */
     881      760628 :   gel(vB, 1) = T;
     882     1619937 :   for (i = 2; i <= l; i++)
     883      859309 :     if (vexp[i])
     884             :     {
     885      798042 :       long j = i >> 1;
     886      798042 :       GEN B2j = r->sqr(E, gel(vB,j));
     887      798042 :       gel(vB,i) = odd(i)? r->mul(E, B2j, T): B2j;
     888             :     }
     889      760628 :   return vB;
     890             : }
     891             : 
     892             : static void
     893      166283 : 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      166283 :   long m = l>>1;
     898      332566 :   if (l==1) { *z=x; return; }
     899       80047 :   q = div(E, x, gel(vB,m), &r);
     900       80047 :   gen_digits_dac(r, vB, m, z, E, div);
     901       80047 :   gen_digits_dac(q, vB, l-m, z+m, E, div);
     902             : }
     903             : 
     904             : static GEN
     905       28889 : 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       28889 :   long m = l>>1;
     911       28889 :   if (l==1) return gel(x,i);
     912       13181 :   a = gen_fromdigits_dac(x, vB, i, m, E, add, mul);
     913       13181 :   b = gen_fromdigits_dac(x, vB, i+m, l-m, E, add, mul);
     914       13181 :   return add(E, a, mul(E, b, gel(vB, m)));
     915             : }
     916             : 
     917             : static GEN
     918        6280 : 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        6280 :   if (n==1) retmkvec(gcopy(x));
     923        6189 :   vB = get_vB(B, n, E, r);
     924        6189 :   z = cgetg(n+1, t_VEC);
     925        6189 :   gen_digits_dac(x, vB, n, (GEN*)(z+1), E, div);
     926        6189 :   return z;
     927             : }
     928             : 
     929             : GEN
     930        6230 : 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        6230 :   pari_sp av = avma;
     934        6230 :   return gerepilecopy(av, gen_digits_i(x, B, n, E, r, div));
     935             : }
     936             : 
     937             : GEN
     938        2527 : gen_fromdigits(GEN x, GEN B, void *E, struct bb_ring *r)
     939             : {
     940        2527 :   pari_sp av = avma;
     941        2527 :   long n = lg(x)-1;
     942        2527 :   GEN vB = get_vB(B, n, E, r);
     943        2527 :   GEN z = gen_fromdigits_dac(x, vB, 1, n, E, r->add, r->mul);
     944        2527 :   return gerepilecopy(av, z);
     945             : }
     946             : 
     947             : static GEN
     948        1771 : _addii(void *data /* ignored */, GEN x, GEN y)
     949        1771 : { (void)data; return addii(x,y); }
     950             : static GEN
     951      771995 : _sqri(void *data /* ignored */, GEN x) { (void)data; return sqri(x); }
     952             : static GEN
     953      200075 : _mulii(void *data /* ignored */, GEN x, GEN y)
     954      200075 :  { (void)data; return mulii(x,y); }
     955             : static GEN
     956         436 : _dvmdii(void *data /* ignored */, GEN x, GEN y, GEN *r)
     957         436 :  { (void)data; return dvmdii(x,y,r); }
     958             : 
     959             : static struct bb_ring Z_ring = { _addii, _mulii, _sqri };
     960             : 
     961             : static GEN
     962         182 : check_basis(GEN B)
     963             : {
     964         182 :   if (!B) return utoipos(10);
     965         161 :   if (typ(B)!=t_INT) pari_err_TYPE("digits",B);
     966         161 :   if (abscmpiu(B,2)<0) pari_err_DOMAIN("digits","B","<",gen_2,B);
     967         161 :   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        3075 : digits_dacsmall(GEN x, GEN vB, long l, ulong* z)
     973             : {
     974        3075 :   pari_sp av = avma;
     975             :   GEN q,r;
     976             :   long m;
     977        6150 :   if (l==1) { *z=itou(x); return; }
     978        1524 :   m=l>>1;
     979        1524 :   q = dvmdii(x, gel(vB,m), &r);
     980        1524 :   digits_dacsmall(q,vB,l-m,z);
     981        1524 :   digits_dacsmall(r,vB,m,z+l-m);
     982        1524 :   avma = av;
     983             : }
     984             : 
     985             : GEN
     986          56 : digits(GEN x, GEN B)
     987             : {
     988          56 :   pari_sp av=avma;
     989             :   long lz;
     990             :   GEN z, vB;
     991          56 :   if (typ(x)!=t_INT) pari_err_TYPE("digits",x);
     992          56 :   B = check_basis(B);
     993          56 :   if (signe(B)<0) pari_err_DOMAIN("digits","B","<",gen_0,B);
     994          56 :   if (!signe(x))       {avma = av; return cgetg(1,t_VEC); }
     995          49 :   if (abscmpii(x,B)<0) {avma = av; retmkvec(absi(x)); }
     996          49 :   if (Z_ispow2(B))
     997             :   {
     998          14 :     long k = expi(B);
     999          14 :     if (k == 1) return binaire(x);
    1000           7 :     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           7 :       avma = av; return binary_2k(x, k);
    1009             :     }
    1010             :   }
    1011          35 :   if (signe(x) < 0) x = absi(x);
    1012          35 :   lz = logint(x,B) + 1;
    1013          35 :   if (lgefint(B)>3)
    1014             :   {
    1015           8 :     z = gerepileupto(av, gen_digits_i(x, B, lz, NULL, &Z_ring, _dvmdii));
    1016           8 :     vecreverse_inplace(z);
    1017           8 :     return z;
    1018             :   }
    1019             :   else
    1020             :   {
    1021          27 :     vB = get_vB(B, lz, NULL, &Z_ring);
    1022          27 :     (void)new_chunk(3*lz); /* HACK */
    1023          27 :     z = zero_zv(lz);
    1024          27 :     digits_dacsmall(x,vB,lz,(ulong*)(z+1));
    1025          27 :     avma = av; return Flv_to_ZV(z);
    1026             :   }
    1027             : }
    1028             : 
    1029             : static GEN
    1030     2497025 : fromdigitsu_dac(GEN x, GEN vB, long i, long l)
    1031             : {
    1032             :   GEN a, b;
    1033     2497025 :   long m = l>>1;
    1034     2497025 :   if (l==1) return utoi(uel(x,i));
    1035     2060455 :   if (l==2) return addumului(uel(x,i), uel(x,i+1), gel(vB, m));
    1036      872570 :   a = fromdigitsu_dac(x, vB, i, m);
    1037      872570 :   b = fromdigitsu_dac(x, vB, i+m, l-m);
    1038      872570 :   return addii(a, mulii(b, gel(vB, m)));
    1039             : }
    1040             : 
    1041             : GEN
    1042      751885 : fromdigitsu(GEN x, GEN B)
    1043             : {
    1044      751885 :   pari_sp av = avma;
    1045      751885 :   long n = lg(x)-1;
    1046             :   GEN vB, z;
    1047      751885 :   if (n==0) return gen_0;
    1048      751885 :   vB = get_vB(B, n, NULL, &Z_ring);
    1049      751885 :   z = fromdigitsu_dac(x, vB, 1, n);
    1050      751885 :   return gerepileuptoint(av, z);
    1051             : }
    1052             : 
    1053             : static int
    1054          14 : ZV_in_range(GEN v, GEN B)
    1055             : {
    1056          14 :   long i, l = lg(v);
    1057        1659 :   for(i=1; i < l; i++)
    1058             :   {
    1059        1652 :     GEN vi = gel(v, i);
    1060        1652 :     if (signe(vi) < 0 || cmpii(vi, B) >= 0)
    1061           7 :       return 0;
    1062             :   }
    1063           7 :   return 1;
    1064             : }
    1065             : 
    1066             : GEN
    1067          56 : fromdigits(GEN x, GEN B)
    1068             : {
    1069          56 :   pari_sp av = avma;
    1070          56 :   if (typ(x)!=t_VEC || !RgV_is_ZV(x)) pari_err_TYPE("fromdigits",x);
    1071          56 :   if (lg(x)==1) return gen_0;
    1072          49 :   B = check_basis(B);
    1073          49 :   if (Z_ispow2(B) && ZV_in_range(x, B))
    1074           7 :     return fromdigits_2k(x, expi(B));
    1075          42 :   x = vecreverse(x);
    1076          42 :   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      355152 : sumdigitsu(ulong n)
    1122             : {
    1123      355152 :   ulong s = 0;
    1124      355152 :   while (n) { s += digsum[n % 1000]; n /= 1000; }
    1125      355152 :   return s;
    1126             : }
    1127             : 
    1128             : /* res=array of 9-digits integers, return \sum_{0 <= i < l} sumdigits(res[i]) */
    1129             : static ulong
    1130          14 : sumdigits_block(ulong *res, long l)
    1131             : {
    1132          14 :   long s = sumdigitsu(*--res);
    1133          14 :   while (--l > 0) s += sumdigitsu(*--res);
    1134          14 :   return s;
    1135             : }
    1136             : 
    1137             : GEN
    1138          35 : sumdigits(GEN n)
    1139             : {
    1140          35 :   pari_sp av = avma;
    1141             :   ulong s, *res;
    1142             :   long l;
    1143             : 
    1144          35 :   if (typ(n) != t_INT) pari_err_TYPE("sumdigits", n);
    1145          35 :   l = lgefint(n);
    1146          35 :   switch(l)
    1147             :   {
    1148           7 :     case 2: return gen_0;
    1149          14 :     case 3: return utoipos(sumdigitsu(n[2]));
    1150             :   }
    1151          14 :   res = convi(n, &l);
    1152          14 :   if ((ulong)l < ULONG_MAX / 81)
    1153             :   {
    1154          14 :     s = sumdigits_block(res, l);
    1155          14 :     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         105 : sumdigits0(GEN x, GEN B)
    1174             : {
    1175         105 :   pari_sp av = avma;
    1176             :   GEN z;
    1177             :   long lz;
    1178             : 
    1179         105 :   if (!B) return sumdigits(x);
    1180          77 :   if (typ(x) != t_INT) pari_err_TYPE("sumdigits", x);
    1181          77 :   B = check_basis(B);
    1182          77 :   if (Z_ispow2(B))
    1183             :   {
    1184          28 :     long k = expi(B);
    1185          28 :     if (k == 1) { avma = av; return utoi(hammingweight(x)); }
    1186          21 :     if (k < BITS_IN_LONG)
    1187             :     {
    1188          14 :       GEN z = binary_2k_nv(x, k);
    1189          14 :       if (lg(z)-1 > 1L<<(BITS_IN_LONG-k)) /* may overflow */
    1190           0 :         return gerepileuptoint(av, ZV_sum(Flv_to_ZV(z)));
    1191          14 :       avma = av; return utoi(zv_sum(z));
    1192             :     }
    1193           7 :     return gerepileuptoint(av, ZV_sum(binary_2k(x, k)));
    1194             :   }
    1195          49 :   if (!signe(x))       { avma = av; return gen_0; }
    1196          49 :   if (abscmpii(x,B)<0) { avma = av; return absi(x); }
    1197          49 :   if (absequaliu(B,10))   { avma = av; return sumdigits(x); }
    1198          42 :   lz = logint(x,B) + 1;
    1199          42 :   z = gen_digits_i(x, B, lz, NULL, &Z_ring, _dvmdii);
    1200          42 :   return gerepileuptoint(av, ZV_sum(z));
    1201             : }

Generated by: LCOV version 1.11