This was a question I had last month:
Part A: Looking at the exponent of each of the elements in the list,
FactorInteger
and usingSelect
construct your own variant ofSquareFreeQ
. Call itsquareFreeQ
. (You want the list of powers inFactorInteger
to be all be 1, equivalently no power should be greater than 1 . The rightSelect
evaluated onFactorInteger
should give{}
. Notice we only have to find one power > 1 so you will speed things up by usingSelect
with an option.Part B: Test that your construction
squareFreeQ
and Mathematica'sSquareFreeQ
are identical. You can do this by selecting the integer for whichSquareFreeQ[n] != squarefreeQ[n]
. Do this on a list of $10^5$ random integers between $10^{13}$ and $10^{14}$. Tell me how long it takes to perform this test. Be prepared to wait a little while.
This is what I had for part A:
squareFreeQ := Select[Last /@ FactorInteger[#], # > 1 &] &
and for part B:
Timing[SquareFreeQ[#] & /@ RandomInteger[{10^13, 10^14}, 10^15]]
squareFreeQ := Select[Last /@ FactorInteger[#], # > 1 &] &
When I got my assignment back, it says incorrect with no explanation. What would be the correct answer? Please explain.