Message Boards Message Boards

GROUPS:

Large time difference when >= is used compared to ==?

Posted 8 years ago
7417 Views
|
3 Replies
|
1 Total Likes
|

Hi Everyone

Is this something expected. In this program

Monitor[Do[a = IntegerDigits[n]; b = RotateLeft[a]; c = RotateLeft[b];
   If[IntegerLength[FromDigits[b]] == IntegerLength[FromDigits[c]] == 
     IntegerLength[n] && Length[Union[b]] == 3 && 
    FromDigits[c] == GeometricMean[{n, FromDigits[b]}], 
   Print["GeometricMean [", n, " , ", FromDigits[b], "] = ", 
    FromDigits[c]]], {n, 100, 1000000}], n]

runs in 19.09 seconds on my system, yet when the Length[Union[b]] == 3 is changed to Length[Union[b]] >= 3 the execution time increases to 164.54 seconds almost 9 times slower. It seems disproportionate to me. Perhaps some one could test it to see if its accurate.

Paul.

POSTED BY: Paul Cleary
3 Replies
Posted 8 years ago

Thanks David and llian, I now see where the difference comes from, regarding the N[n] that certainly speeded it up, the timings went down to 8.61 and 51.76 respectively.

POSTED BY: Paul Cleary

Note that And uses short-circuit evaluation, and count for how many numbers computing the GeometricMean will be necessary:

  Length[Select[
    Range[100, 
     1000000], (a = IntegerDigits[#]; b = RotateLeft[a]; 
      c = RotateLeft[b]; 
      IntegerLength[FromDigits[b]] == IntegerLength[FromDigits[c]] == 
        IntegerLength[#] && Length[Union[b]] >= 3) &]]

(* Out[17]= 805752 *)

but if the last condition used ==, there would only be 67896 -- much less work.

Also, the timings should improve if you use GeometricMean[{N[n], FromDigits[b]}], since machine precision computation is faster.

POSTED BY: Ilian Gachevski
Posted 8 years ago

On my Core i5 laptop it goes from 33s to 268s. About the same ratio.

POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract