Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: a close prime number

by RazorbladeBidet (Friar)
on Feb 14, 2005 at 19:01 UTC ( #430888=note: print w/ replies, xml ) Need Help??


in reply to Re^2: a close prime number
in thread a close prime number

I wonder if it's quicker to add the digits of the number when you're checking to see if it's divisible by three and seeing if that resultant sum is divisible by three.


Comment on Re^3: a close prime number
Re^4: a close prime number
by dragonchild (Archbishop) on Feb 14, 2005 at 19:11 UTC
    It's not.
    use Benchmark qw( cmpthese ); my $large_number = 3 * 1234647389; cmpthese( -1, { modulo => sub { $large_number % 3 == 0 }, divide => sub { my $v; $v += $_ for split //, $large_number; $v % +3 == 0}, }); -------- Rate divide modulo divide 30919/s -- -99% modulo 2120579/s 6759% --

    By a factor of 67 times faster. Or so ...

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      Darn - so !(faster in my head == faster in code) :)

      Thanks for the benchmarks, dragonchild.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://430888]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2014-12-25 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls