Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Number functions I have lying around

by Discipulus (Canon)
on Mar 31, 2015 at 11:04 UTC ( [id://1121960]=note: print w/replies, xml ) Need Help??


in reply to Re: Number functions I have lying around
in thread Number functions I have lying around

And you can profit of an enhencemt if you too add if($i%2==0){next} before eleborating the square root, as you can see in the ch_opt row.
Rate la di ch ch_opt la 0.329/s -- -99% -99% -99% di 25.3/s 7600% -- -50% -56% ch 50.4/s 15230% 99% -- -12% ch_opt 57.6/s 17417% 127% 14% --

L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^3: Number functions I have lying around
by Anonymous Monk on Apr 02, 2015 at 16:39 UTC

    A modest improvement in speed may be obtained if one implements the wheel.

    For example (the modulus is relatively cheap in perl):

    sub primes { my $n = shift; return if $n < 2; my @primes = (2); I: for my $i (3 .. $n) { next unless 0x208a28aa & (1 << $i % 30); my $sqrt = int sqrt $i; for my $p (@primes) { next I unless $i % $p; last if $p > $sqrt; } push @primes, $i; } return @primes }

      Actually, it's 0x208a2882, but you have to initialize primes to (2, 3, 5) then..

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-23 05:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found