Karim Belabas on Sun, 19 Dec 2021 16:38:09 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: factorint_as_string()


* Bill Allombert [2021-12-19 15:58]:
> On Sun, Dec 19, 2021 at 03:24:15PM +0100, Ruud H.G. van Tol wrote:
> > 
> > sf() = my(v=Vec(factorint(n)~)); if(!#v, v=Vec([1;1])); for(i=1, #v,
> > v[i]=if(v[i][2]>1, strjoin(v[i],"^"), v[i][1])); strjoin(["(", strjoin(v," *
> > "), ")"])
> 
> Hello Ruud, thanks for your code!
> 
> You forgot to list the argument name (n) in sf(n)
> 
> You can use Str instead of the second strjoin:
> 
> sf(n) =
> {
>    my(v=Vec(factorint(n)~)); 
>    if(!#v, v=Vec([1;1])); 
>    for(i=1, #v,
>      v[i]=if(v[i][2]>1, strjoin(v[i],"^"), v[i][1])); 
>    Str("(", strjoin(v," * "), ")")
> }

Suggestions for increased readability:
  sf(n) =
  { my (v, w);
    v = if (n == 1, [[1,1]], Vec(factorint(n)~));
    w = vector(#v, i, my([p,e] = v[i]);
                      if (e > 1, Str(p, "^", e), p));
    Str("(", strjoin(w, " * "), ")");
  }

  (16:35) gp > sf(24432890625000)
  %1 = "(2^3 * 3^7 * 5^10 * 11 * 13)"

Cheers,

    K.B.
--
Karim Belabas, IMB (UMR 5251)  Tel: (+33) (0)5 40 00 26 17
Universite de Bordeaux         Fax: (+33) (0)5 40 00 21 23
351, cours de la Liberation    http://www.math.u-bordeaux.fr/~kbelabas/
F-33405 Talence (France)       http://pari.math.u-bordeaux.fr/  [PARI/GP]
`