# Testing the Erdos square free conjecture

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.

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

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.

-

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.

-