Re: The prime factors of n.
- To: mathgroup at smc.vnet.net
- Subject: [mg32733] Re: [mg32673] The prime factors of n.
- From: Richard Palmer <mapsinc at bellatlantic.net>
- Date: Thu, 7 Feb 2002 05:10:48 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
on 2/6/02 3:41 AM, Robert G. Wilson v at rgwv at kspaint.com wrote:
> Hello all,
>
> I wish to receive a list of prime factors of n not in the form
> returned by FactorInteger. Instead I want only the primes the number of
> times they appear. As an example I will use 72. FactorInteger[72] gives
> { {2,3}, {3,2} }. I wish the list would read { 2, 2, 2, 3, 3 }. Is the
> following the best that I can do? f[n_Integer] := Module[{a =
> FactorInteger[n], b = {}}, While[Length[a] > 0, Do[b = Append[b, a[[1,
> 1]]], {a[[1, 2]]}]; a = Drop[a, 1]]; b] .
>
> See
> http://www.research.att.com/cgi-bin/access.cgi/as/njas/sequences/eisA.cgi?Anum
> =037276
>
> Sincerely yours,
>
> Robert G. "Bob" Wilson, V
>
>
Hi Bob,
Try the following. Define the function f. Obviously you can call it
anything you want, and make the proper substitution later.
> f[{a_, b_}] := Table[a, {b}]
This makes a list of b a's where f[{3,2}] would produce {3,3}.
Now map the function f to the output from factor integer and flatten the
result.
> Flatten[f /@ {{2, 3}, {3, 2}}]
This is not the most efficient way to do this because you are using tables
and flatten, but it will be plenty fast for your work.
Richard Palmer