list of factors [was: Maple question]

cino hilliard hillcino368 at hotmail.com
Sun May 2 08:01:09 CEST 2004


Hi,

>From: "Eric W. Weisstein" <eww at wolfram.com>
>To: Emeric Deutsch <deutsch at duke.poly.edu>,        "N. J. A. Sloane" 
><njas at research.att.com>
>CC: seqfan at ext.jussieu.fr
>Subject: RE: list of factors [was: Maple question]
>Date: Sat, 1 May 2004 09:01:57 -0500 (CDT)
>
>On Sat, 1 May 2004, Emeric Deutsch wrote:
>
> > Dear Seqfans,
> > I wonder whether this is an appropriate topic.
> > Let me formulate my simple question on an example.
> > The command ifactor(72) yields 2^3 3^2.
> > I would like to get [2,2,2,3,3].
> > Any idea? Thanks.
> > Emeric
>
>On Sat, 1 May 2004, N. J. A. Sloane wrote:
>
> > > ifactors(72);
> >                                             [1, [[2, 3], [3, 2]]]
> >
> > is as close as you can get - but that's pretty close!
> >
> > Emeric asked:
> > The command ifactor(72) yields 2^3 3^2.
> > I would like to get [2,2,2,3,3].
>
>In Mathematica:
>
>Flatten[Table[#1,{#2}]& @@@ FactorInteger[72]]
>   or
>Flatten[Table[#[[1]], {#[[2]]}]& /@ FactorInteger[72]]
>   or
>Flatten[Table[First[#],{Last[#]}]& /@ FactorInteger[72]]
>
>{2,2,2,3,3}
>
>Cheers,
>-Eric
>

In Pari:

ifactor(n) =
           {
            local(f,j,k,fstr);
            fstr="";
            f=Vec(factor(n));
            for(j=1,length(f[1]),
               for(k = 1,f[2][j],fstr = 
concat(concat(fstr,Str(f[1][j])),",");
                );
               );
            fstr = mid(fstr,1,length(fstr)-1);
            print("["fstr"]")
           }

\\ Get a substring of length n from string str starting at position s in 
str.
mid(str,s,n) =
                {
                local(v,ln,x,tmp);
                v ="";
                tmp = Vec(str);
                ln=length(tmp);
                for(x=s,s+n-1,
                v=concat(v,tmp[x]);
                );
                return(v)
                }

Having learned from Pari here is a maple rendition  Maple:

ifactor2 := proc(n)
local f,A,j,k,ln,x,fstr,nstr;
f:=ifactors(n);
fstr:="";
x:=f[2];
A:=Array(x);
ln := ArrayNumElems(A)/2;
   for j from 1 to ln do
     for j from 1 to f[2,j][2] do
       fstr := cat(fstr,convert(f[2,j][1],string),",");
     end do;
   end do;
nstr:=cat("[",substring(fstr,1..length(fstr)-1),"]");
printf(nstr);
end proc;

Cino

_________________________________________________________________
Check out the coupons and bargains on MSN Offers! http://youroffers.msn.com






More information about the SeqFan mailing list