2
$\begingroup$

The following function io[aIn_] produces a list bOut containing ALL the prime factors of an Integer via the standard function FactorInteger[]. My code is:

io[aIn_] := 
(bOut = {};fi = FactorInteger[aIn];Do[bOut~AppendTo~(PadLeft[{}, fi[[i, 2]], fi[[i, 1]]] ), {i, 1, Length[fi]}]; bOut = bOut // Flatten )

I would like to know whether there are other solutions for this "problem"?

My solution does not work with e.g. aIn={12321}; io[aIn] nor with io[{12321}] or io[{12, 21}] and I do not understand all the warnings given by MMA.

I do not like to build a function that treats the integers of an input seperately and combines the results into a single list.
I prefer a solution for io[] with "list in of composite integers" gives " list out of all prime factors".

$\endgroup$
6
  • $\begingroup$ Surprised no one has picked this up yet. Lots of ways, here's one: SetAttributes[f, Listable]; f[n_] := Sequence @@ ConstantArray[#1, #2] & @@@ FactorInteger[n] $\endgroup$
    – Aky
    Commented Mar 17, 2014 at 15:41
  • $\begingroup$ I'm not sure from your last sentence whether you want all the factors of a list of composite numbers to be in a single list, or whether each number's factors should appear in its own list. My solution does the latter, but you can Apply Join to my solution to get the former. Or you could just multiply the input numbers together first.. $\endgroup$
    – Aky
    Commented Mar 17, 2014 at 15:46
  • $\begingroup$ @Aky: Thank you, I learned from your first comment how to make my function io Listable with SetAttributes. My solution does work now. $\endgroup$
    – Rombert
    Commented Mar 17, 2014 at 19:54
  • $\begingroup$ @Aky: Will you please make from your second comment an answer. The use of Sequence was new for me. $\endgroup$
    – Rombert
    Commented Mar 17, 2014 at 20:03
  • $\begingroup$ You're welcome. You've probably realised that rasher's solution, while being the same approach as mine, is cleaner - the pure function ConstantArray[#1,# 2]& I used was redundant. $\endgroup$
    – Aky
    Commented Mar 18, 2014 at 5:54

2 Answers 2

2
$\begingroup$

Produces same results as your example:

io[n_] := Join @@ ConstantArray @@@ FactorInteger[n]

No real advantage other than cleaner and lower memory use, since yours is fast already.

To get list of all factors of a list of integers:

io[n_] := Join @@ ConstantArray @@@ Join @@ FactorInteger /@ n

No need to set any attributes doing it this way. Prepend Sort if you want all factors from all numbers in submitted list grouped, e.g.:

io[n_] := Sort@(Join @@ ConstantArray @@@ Join @@ FactorInteger /@ n)
$\endgroup$
0
2
$\begingroup$

The prime factors of an input integernare returned byFactorIntegerin the first positions of a list of pairs. The second position in each pair is the exponent of the corresponding prime, which you want to discard.

PrimeFactors[n_Integer]:=FactorInteger[n][[All, 1]]

For an input list of integersn, find the prime factors from each integer in the input list, and then find theUnionof the result. Note thatFactorIntegeraccepts lists of integers as well as single integers.

PrimeFactors[n_List]:=Union[Flatten[FactorInteger[n][[All, All, 1]]]]

This is my interpretation of the last sentence of your post. However, the second last sentence appears to contradict my assumption. Perhaps you want the prime factors of each integer in the input listnto be in a separate list. Then you could use

PrimeFactors[n_List] := FactorInteger[n][[All, All, 1]]
$\endgroup$
2
  • $\begingroup$ I think he wants the various prime factors in their respective multiplicities, rather than just the unique ones. $\endgroup$
    – Aky
    Commented Mar 17, 2014 at 15:49
  • $\begingroup$ @KennyColnago: As Aky wrote, I want in the outputlist All factors, in the inputlist the composite integers $\endgroup$
    – Rombert
    Commented Mar 18, 2014 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.