diff options
| author | E. Choroba <choroba@matfyz.cz> | 2019-06-15 00:23:02 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2019-06-15 00:23:02 +0200 |
| commit | 268ca6c739b79e7835a31c994b3bd435fdbf790a (patch) | |
| tree | 76ecae3c6339bf1b7f6c8dfdfe68eda7395340d8 /challenge-012 | |
| parent | 8277ae261803351bd2af64e8644170d2c3415ff9 (diff) | |
| download | perlweeklychallenge-club-268ca6c739b79e7835a31c994b3bd435fdbf790a.tar.gz perlweeklychallenge-club-268ca6c739b79e7835a31c994b3bd435fdbf790a.tar.bz2 perlweeklychallenge-club-268ca6c739b79e7835a31c994b3bd435fdbf790a.zip | |
Add solutions of 012 plus a link to the blog post
Diffstat (limited to 'challenge-012')
| -rw-r--r-- | challenge-012/e-choroba/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-012/e-choroba/perl5/ch-1.pl | 80 | ||||
| -rwxr-xr-x | challenge-012/e-choroba/perl5/ch-2.pl | 25 |
3 files changed, 106 insertions, 0 deletions
diff --git a/challenge-012/e-choroba/blog.txt b/challenge-012/e-choroba/blog.txt new file mode 100644 index 0000000000..baeb859f4b --- /dev/null +++ b/challenge-012/e-choroba/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/e_choroba/2019/06/perl-weekly-challenge-012-non-prime-euclid-numbers-and-the-common-path.html diff --git a/challenge-012/e-choroba/perl5/ch-1.pl b/challenge-012/e-choroba/perl5/ch-1.pl new file mode 100755 index 0000000000..f4f82e205c --- /dev/null +++ b/challenge-012/e-choroba/perl5/ch-1.pl @@ -0,0 +1,80 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; + +use List::Util qw{ product }; + +{ package My::Primes; + + sub new { bless [2], shift } + + sub is_prime { + my ($self, $n) = @_; + + $self->extend_to($n) if $n > $self->[-1]; + return grep $_ == $n, @$self + } + + sub is_prime2 { + my ($self, $n) = @_; + + if ($n > $self->[-1]) { + $self->extend_to($n); + return $self->[-1] == $n + + } else { + my ($from, $to) = (0, $#$self); + while ($from != $to) { + my $between = int(($from + $to) / 2); + if ($n > $self->[$between]) { + $from = $between + 1; + } else { + $to = $between; + } + } + return $self->[$from] == $n + } + } + + sub extend_to { + my ($self, $n) = @_; + for my $p ($self->[-1] + 1 .. $n) { + my ($i, $is) = (0, 1); + while ($self->[$i] <= sqrt $p) { + $is = 0, last if 0 == $p % $self->[$i++]; + } + push @$self, $p if $is; + } + } + + sub size { scalar @{ $_[0] } } +} + +#### + +{ use Test::More; + + my $p = 'My::Primes'->new; + + ok $p->is_prime(2); + ok $p->is_prime(101); + + $p->extend_to(2060); + is $p->[-1], 2053; + + done_testing(); +} + +#### + +my $p = 'My::Primes'->new; +my $size = 1; +my $e = 3; +while ($p->is_prime($e)) { + ++$size; + $e = 1 + product(@$p[0 .. $size - 1]); +} + +say $e; + diff --git a/challenge-012/e-choroba/perl5/ch-2.pl b/challenge-012/e-choroba/perl5/ch-2.pl new file mode 100755 index 0000000000..b0777fb95b --- /dev/null +++ b/challenge-012/e-choroba/perl5/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; + +use List::Util qw{ first }; + +my @paths = qw( + /a/b/c/d + /a/b/cd + /a/b/cc + /a/b/c/d/e +); + +my @p1 = split m{/}, $paths[0]; +my $min = @p1; +for my $i (1 .. $#paths) { + my @p2 = split m{/}, $paths[$i]; + my $max = (@p1 < @p2) ? $#p1 : $#p2; + my $diff = first { $p1[$_] ne $p2[$_] } 0 .. $max; + $diff //= $max + 1; + $min = $diff if $diff < $min; +} + +say join '/', (split m{/}, $paths[0])[0 .. $min - 1]; |
