Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

The following works:

list = {6, 7, 8, 9};
FreeQ2[n_Integer] := FreeQ[Last@Transpose@FactorInteger[n], _?(# >= 2 &)]
FreeQ3[n_Integer] := FreeQ[Last@Transpose@FactorInteger[n], _?(# >= 3 &)]
FreeQ4[n_Integer] := FreeQ[Last@Transpose@FactorInteger[n], _?(# >= 4 &)]
a = FreeQ2 /@ list /. {True -> 1, False -> 0};
b = FreeQ3 /@ list /. {True -> 2, False -> 0};
c = FreeQ4 /@ list /. {True -> 3, False -> 0};
Transpose[{a, b, c}] /. {{1, 2, 3} -> 1, {0, 2, 3} -> 2, {0, 0, 3} -> 3}

but of course is very inefficient coding. I was wondering what approach I should really be taking here.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

I wanted a way without factorizing the numbers, but found none. So:

list = {6, 7, 8, 9};
Max /@ FactorInteger[list][[All, All, 2]]

(*
{1,1,3,2}
*)

Let's check the timings in my old laptop:

list = RandomInteger[10^6, 10^6];
Max /@ FactorInteger[list][[All, All, 2]] // Timing // First
(*
  7 secs
*)
share|improve this answer
    
@ belisarius, this solution seems most straightforward to me - many thanks for your time :) –  martin Nov 27 '13 at 10:46

Mathematica essentially has FreeQ2 built in:

FreeQ2[n_Integer] := MoebiusMu[n] != 0

Here's an idea for the rest. This depends on factoring an integer, but maybe something like

FreeQPow[n_, e_] := Head[n^(1/e)] === Power
share|improve this answer
3  
Mathematica already has a built-in SquareFreeQ[n] –  belisarius Nov 27 '13 at 2:11
    
@ RiemannZeta, thanks for your help on this - the bottom power solution is a nice start for me :) –  martin Nov 27 '13 at 10:56

How about

pwrs = Flatten[Max /@ DeleteDuplicates@Last@Transpose@FactorInteger[#] & /@ list,1];
Transpose[{list,pwrs}]
share|improve this answer
    
Great - thanks for your help on this - I was trying to work out how to stack the transpose commands :) –  martin Nov 27 '13 at 10:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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