Sha256: 0312c9d37e9c1db15f1225fcbed88bfae85b4c7dbf2147066b5098a0a5983616

Contents?: true

Size: 557 Bytes

Versions: 73

Compression:

Stored size: 557 Bytes

Contents

package Sieve;

use strict;
use warnings;

sub new {
    my ($class, $max) = @_;
    die "ArgumentError, number too small: $max" if $max < 2;
    bless { max => $max }, $class;
}

sub primes {
    my $self = shift;
    my $prime = 2;

    return [$prime] if $self->{max} == $prime;

    my @list = 3..$self->{max};
    my @primes = ($prime,);

    while(@list > 0) {
        my @candidates = grep { $_ % $prime != 0 } @list;
        $prime = shift @candidates;
        push @primes => $prime;
        @list = @candidates;
    }

    return [@primes];
}

1;

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/perl5/exercises/sieve/.meta/solutions/Sieve.pm
trackler-2.2.1.118 tracks/perl5/exercises/sieve/.meta/solutions/Sieve.pm
trackler-2.2.1.117 tracks/perl5/exercises/sieve/.meta/solutions/Sieve.pm
trackler-2.2.1.116 tracks/perl5/exercises/sieve/.meta/solutions/Sieve.pm
trackler-2.2.1.115 tracks/perl5/exercises/sieve/.meta/solutions/Sieve.pm
trackler-2.2.1.114 tracks/perl5/exercises/sieve/.meta/solutions/Sieve.pm
trackler-2.2.1.113 tracks/perl5/exercises/sieve/.meta/solutions/Sieve.pm
trackler-2.2.1.111 tracks/perl5/exercises/sieve/.meta/solutions/Sieve.pm
trackler-2.2.1.110 tracks/perl5/exercises/sieve/.meta/solutions/Sieve.pm
trackler-2.2.1.109 tracks/perl5/exercises/sieve/.meta/solutions/Sieve.pm
trackler-2.2.1.108 tracks/perl5/exercises/sieve/.meta/solutions/Sieve.pm
trackler-2.2.1.107 tracks/perl5/exercises/sieve/.meta/solutions/Sieve.pm
trackler-2.2.1.106 tracks/perl5/exercises/sieve/.meta/solutions/Sieve.pm