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.

Can someone please answer these two parts of my homework assignment? Adding an explanation would be appreciated!

a. Test the Erdos square free conjecture for $n <= 30000$. You should use SquareFreeQ. Give the time it takes to perform this test.

For this part, I said,

{#, SquareFreeQ[#]} & /@ {Binomial[5, 30000]}

b. Now test using your squareFreeQ and give the time it takes to perform this test.

For this part, my squareFreeQ is:

squareFreeQ[n_Integer] := Select[FactorInteger[n][[All, 2]], # > 1 &] == {}

For part b, my answer is:

{#, squareFreeQ[#]} & /@ {Binomial[5, 30000]}

But none of the answers seem to be correct.

share|improve this question
1  
Welcome! If this is homework then you should add the homework tag. –  Yves Klett Apr 14 at 5:08
    
Binomial[5, 30000] gives only the binomial for $n=30000$ instead of $n\leq 30000$. –  Silvia Apr 14 at 7:18

2 Answers 2

part (a)

data = Table[n, {n, 5, 3000}];
Timing[
 res = {#, SquareFreeQ[Binomial[2 #, #]]} & /@ data;
 Cases[res, {n_, True}]
 ]

(* {0.280802, {}} *)

part (b)

data = Table[n, {n, 5, 3000}];
mySquareFreeQ[n_Integer] := TrueQ[Length[Cases[FactorInteger[n], 
                                         {p_, m_} /; m > 1 :> True]] == 0];
Timing[
 res = {#, mySquareFreeQ[Binomial[2 #, #]]} & /@ data;
 Cases[res, {n_, True}]
 ]

(* {2.215214, {}} *)

Mathematica wins easily. I used 3000 instead of 30000 because my implementation is much slower than M and it was taking too long.

share|improve this answer

My implementation of mySquareFreeQ probably comes under the heading of "not what the teacher intended" :-)

mySquareFreeQ = Thread[MoebiusMu@# != 0] &

Timings:

data = Binomial[2 #, #] &@Range[5, 30000];

Timing[Nor @@ SquareFreeQ /@ data]
(* {0.140625, True} *)

Timing[Nor @@ mySquareFreeQ @ data]
(* {0.078125, True} *)

So my implementation is about twice as fast as using the built-in SquareFreeQ.

Explanation

I cheated, of course. MoebiusMu is what SquareFreeQ uses internally (MoebiusMu returns zero if the argument has multiple prime factors). However, MoebiusMu is Listable whereas SquareFreeQ is not. So it is somewhat faster to evaluate MoebiusMu with a list argument than it is to explicitly map SquareFreeQ over the list.

share|improve this answer

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.