Ruud H.G. van Tol on Mon, 20 Dec 2021 10:14:06 +0100


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

Re: factorint_as_string()



On 2021-12-19 16:38, Karim Belabas wrote:
* 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)

Oops.


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)"
Thanks! Have been looking for advanced PARI-coding-FAQs, but failed.

So if you have such pointers, or know a central place where those are collected, please let me know. Also libraries with handy functions.

For me it is easy to create what I need, but good examples always help.

I like using Perl for quick scripts, and Perl and PARI go together quite well.

-- Ruud