aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-06-17 14:35:56 +0100
committerGitHub <noreply@github.com>2019-06-17 14:35:56 +0100
commitce67d818ae31a56a05d1ac9369e2257f84575952 (patch)
tree081dc8ec7ee060ccc227ad9f939d4141131a62e4
parent13fe7935be7c9068467427684afa2d72ac3f1b8c (diff)
parent3db51a9f35ae3bd69ba457ad6213053ef82fa60a (diff)
downloadperlweeklychallenge-club-ce67d818ae31a56a05d1ac9369e2257f84575952.tar.gz
perlweeklychallenge-club-ce67d818ae31a56a05d1ac9369e2257f84575952.tar.bz2
perlweeklychallenge-club-ce67d818ae31a56a05d1ac9369e2257f84575952.zip
Merge pull request #269 from joetym/jtym-challenge12
further tweaks + benchmarks added
-rw-r--r--challenge-012/joe-tym/perl5/ch-1.pl20
1 files changed, 15 insertions, 5 deletions
diff --git a/challenge-012/joe-tym/perl5/ch-1.pl b/challenge-012/joe-tym/perl5/ch-1.pl
index d5ad7a36eb..d68aa05667 100644
--- a/challenge-012/joe-tym/perl5/ch-1.pl
+++ b/challenge-012/joe-tym/perl5/ch-1.pl
@@ -9,13 +9,21 @@ use Data::Dumper;
http://blogs.perl.org/users/laurent_r/2019/06/perl-weekly-challenge-12-euclids-numbers-and-directories.html
Refactored to use Perl Data Language(PDL), Perl's numpy equivalent, to calculate prime numbers
+
+ Benchmark results, to generate primes numbers 0-10 million:
+
+ Pure Perl implementation(above) -> 0m26.662s
+ PDL implementation(below) -> 0m0.284s
+ NUMPY(1.8.2) implementation: -> 0m0.129s
=cut
main();
sub main {
- my $primes = primesfrom2to(31000);
+ #$PDL::BIGPDL = 1;
+ my $primes = primesfrom2to(10000000);
+ #print $primes->dim(0)."\n";
#print $primes->where($primes > 30000); exit;
#print is_prime($primes, 13);
my @prime_numbers = $primes->list();
@@ -35,7 +43,7 @@ sub main {
}
=head1
-https://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n=head1
+https://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n
import numpy
def primesfrom2to(n):
@@ -51,9 +59,11 @@ def primesfrom2to(n):
sub primesfrom2to {
use integer;
+ #my $x = 11 / 3;
+ #print Dumper($x); exit;
my $n = shift || die 'input expected';
my $sieve = ones($n/3 + ($n % 6 == 2));
- my $end = int($n**0.5)/3+1 - 1;
+ my $end = int($n**0.5)/3+1;
my $last = $sieve->dim(0) - 1;
foreach my $i (1..$end) {
if ($sieve->at($i) == 1) {
@@ -63,9 +73,9 @@ sub primesfrom2to {
my $step = 2*$k;
#print "$k $start1 $start2 $step\n";
- #if ($start1 <= $last) {
+ if ($start1 <= $last) {
$sieve->slice($start1.':'.$last.':'.$step) .= 0;
- #}
+ }
if ($start2 <= $last) {
$sieve->slice($start2.':'.$last.':'.$step) .= 0;
}