Skip to main content
added 434 characters in body
Source Link
Pinguin Dirk
  • 6.5k
  • 1
  • 26
  • 36

I think that @KennyColnago is right, you need to make sure you return True or False. Additionally, in the last sentence in part A your prof is pointing out that you should use an option to make sure Select quits the routine as soon as a power is found, thus you can (and possibly should) use the third argument (strictly speaking not an "option") of Select:

squareFreeQ := Select[Last /@ FactorInteger[#], # > 1 &, 1] == {} &

(I currently do not see any option to improve the speed in I am using FactorInteger, i.e. shortcutting this here as well, but your prof explicitly askspoints out, knowing that this won't work for itexpressions like the ones mentioned in @bills answer.

Then for Part B, so beyou could do either like in KennyColnago's idea:

With[{r=RandomInteger[{10^13,10^14},10^5]}, 
   AbsoluteTiming[Select[r, squareFreeQ[#]=!=SquareFreeQ[#]&,1]]]

(where we use again the 3rd argument in Select, to get the first exception only - but as we are confident, we don't really need it. here. Just to illustrate the point.)

ThenAlternatively:

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

Both are just about the same speed, and:

res1 == res2

True

I think that @KennyColnago is right, you need to make sure you return True or False. Additionally, in the last sentence in part A your prof is pointing out that you should use an option to make sure Select quits the routine as soon as a power is found, thus you can (and possibly should) use the third argument (strictly speaking not an "option") of Select:

squareFreeQ := Select[Last /@ FactorInteger[#], # > 1 &, 1] == {} &

(I currently do not see any option to improve the speed in FactorInteger, i.e. shortcutting this as well, but your prof explicitly asks for it, so be it...)

Then:

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

Both are just about the same speed, and:

res1 == res2

True

I think that @KennyColnago is right, you need to make sure you return True or False. Additionally, in the last sentence in part A your prof is pointing out that you should use an option to make sure Select quits the routine as soon as a power is found, thus you can (and possibly should) use the third argument (strictly speaking not an "option") of Select:

squareFreeQ := Select[Last /@ FactorInteger[#], # > 1 &, 1] == {} &

I am using FactorInteger here as your prof points out, knowing that this won't work for expressions like the ones mentioned in @bills answer.

Then for Part B, you could do either like in KennyColnago's idea:

With[{r=RandomInteger[{10^13,10^14},10^5]}, 
   AbsoluteTiming[Select[r, squareFreeQ[#]=!=SquareFreeQ[#]&,1]]]

(where we use again the 3rd argument in Select, to get the first exception only - but as we are confident, we don't really need it here. Just to illustrate the point.)

Alternatively:

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

Both are just about the same speed, and:

res1 == res2

True

Source Link
Pinguin Dirk
  • 6.5k
  • 1
  • 26
  • 36

I think that @KennyColnago is right, you need to make sure you return True or False. Additionally, in the last sentence in part A your prof is pointing out that you should use an option to make sure Select quits the routine as soon as a power is found, thus you can (and possibly should) use the third argument (strictly speaking not an "option") of Select:

squareFreeQ := Select[Last /@ FactorInteger[#], # > 1 &, 1] == {} &

(I currently do not see any option to improve the speed in FactorInteger, i.e. shortcutting this as well, but your prof explicitly asks for it, so be it...)

Then:

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

Both are just about the same speed, and:

res1 == res2

True