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

Posted 8 years ago
7417 Views
|
3 Replies
|
1 Total Likes
|
 Hi EveryoneIs 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.
3 Replies
Sort By:
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 8 years ago
 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 8 years ago
 On my Core i5 laptop it goes from 33s to 268s. About the same ratio.