Skip to main content
added 1 characters in body
Source Link
Stefan
  • 5.4k
  • 26
  • 32

Maybe too late to join the party, but here's is my implementation for squareFreeQ.

We know that a number is square-free, if its prime decomposition contains no repeated factors. Also we've to take into account, that 1 is by convention squarefree.

We can list the first squarefree numbers using:

Select[Range[20], Max[Last /@ FactorInteger[#]] < 2 &]

and we could compare this with Sloanes sequence A005117

Having this, an implementation seems to be rather straight forward:

squareFreeQ[x_] := Max[Last /@ FactorInteger[x]] < 2

squareFreeQ[11] ==> True
squareFreeQ[12] ==> False

Timings are:

rInts = RandomInteger[{10^13, 10^14}, 10^5];
(res1 = SquareFreeQ /@ rInts;) // AbsoluteTiming
(res2 = squareFreeQ /@ rInts;) // AbsoluteTiming  

{34.255805, Null}
{39.001603, Null}

I'm loosing 5 seconds when compared to the internal implementation, but I'm sure that squareFreeQ could be tweaked...

Maybe too late to join the party, but here's is my implementation for squareFreeQ.

We know that a number is square-free, if its prime decomposition contains no repeated factors. Also we've to take into account, that 1 is by convention squarefree.

We can list the first squarefree numbers using:

Select[Range[20], Max[Last /@ FactorInteger[#]] < 2 &]

and we could compare this with Sloanes sequence A005117

Having this an implementation seems to be rather straight forward:

squareFreeQ[x_] := Max[Last /@ FactorInteger[x]] < 2

squareFreeQ[11] ==> True
squareFreeQ[12] ==> False

Timings are:

rInts = RandomInteger[{10^13, 10^14}, 10^5];
(res1 = SquareFreeQ /@ rInts;) // AbsoluteTiming
(res2 = squareFreeQ /@ rInts;) // AbsoluteTiming  

{34.255805, Null}
{39.001603, Null}

I'm loosing 5 seconds when compared to the internal implementation, but I'm sure that squareFreeQ could be tweaked...

Maybe too late to join the party, but here's is my implementation for squareFreeQ.

We know that a number is square-free, if its prime decomposition contains no repeated factors. Also we've to take into account, that 1 is by convention squarefree.

We can list the first squarefree numbers using:

Select[Range[20], Max[Last /@ FactorInteger[#]] < 2 &]

and we could compare this with Sloanes sequence A005117

Having this, an implementation seems to be rather straight forward:

squareFreeQ[x_] := Max[Last /@ FactorInteger[x]] < 2

squareFreeQ[11] ==> True
squareFreeQ[12] ==> False

Timings are:

rInts = RandomInteger[{10^13, 10^14}, 10^5];
(res1 = SquareFreeQ /@ rInts;) // AbsoluteTiming
(res2 = squareFreeQ /@ rInts;) // AbsoluteTiming  

{34.255805, Null}
{39.001603, Null}

I'm loosing 5 seconds when compared to the internal implementation, but I'm sure that squareFreeQ could be tweaked...

Source Link
Stefan
  • 5.4k
  • 26
  • 32

Maybe too late to join the party, but here's is my implementation for squareFreeQ.

We know that a number is square-free, if its prime decomposition contains no repeated factors. Also we've to take into account, that 1 is by convention squarefree.

We can list the first squarefree numbers using:

Select[Range[20], Max[Last /@ FactorInteger[#]] < 2 &]

and we could compare this with Sloanes sequence A005117

Having this an implementation seems to be rather straight forward:

squareFreeQ[x_] := Max[Last /@ FactorInteger[x]] < 2

squareFreeQ[11] ==> True
squareFreeQ[12] ==> False

Timings are:

rInts = RandomInteger[{10^13, 10^14}, 10^5];
(res1 = SquareFreeQ /@ rInts;) // AbsoluteTiming
(res2 = squareFreeQ /@ rInts;) // AbsoluteTiming  

{34.255805, Null}
{39.001603, Null}

I'm loosing 5 seconds when compared to the internal implementation, but I'm sure that squareFreeQ could be tweaked...