diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-03-05 10:08:11 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-03-05 10:08:11 +0000 |
| commit | 7770ebc3fa44412a814a2aaa5a8a81d57b52663f (patch) | |
| tree | 1ab0a1344613d0cc1da66466ac8e96f7238a1c8c /challenge-154 | |
| parent | db09f8680b6d72ddc88d037eae80de86ca0d71a6 (diff) | |
| download | perlweeklychallenge-club-7770ebc3fa44412a814a2aaa5a8a81d57b52663f.tar.gz perlweeklychallenge-club-7770ebc3fa44412a814a2aaa5a8a81d57b52663f.tar.bz2 perlweeklychallenge-club-7770ebc3fa44412a814a2aaa5a8a81d57b52663f.zip | |
- Added solutions by Laurent Rosenfeld.
Diffstat (limited to 'challenge-154')
| -rw-r--r-- | challenge-154/laurent-rosenfeld/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-154/laurent-rosenfeld/perl/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-154/laurent-rosenfeld/perl/ch-2.pl | 50 | ||||
| -rw-r--r-- | challenge-154/laurent-rosenfeld/raku/ch-1.raku | 7 | ||||
| -rw-r--r-- | challenge-154/laurent-rosenfeld/raku/ch-1a.raku | 6 | ||||
| -rw-r--r-- | challenge-154/laurent-rosenfeld/raku/ch-2.raku | 15 |
6 files changed, 102 insertions, 0 deletions
diff --git a/challenge-154/laurent-rosenfeld/blog.txt b/challenge-154/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..c567d36e2e --- /dev/null +++ b/challenge-154/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/laurent_r/2022/03/perl-weekly-challenge-154-missing-permutations-and-padovan-primes.html diff --git a/challenge-154/laurent-rosenfeld/perl/ch-1.pl b/challenge-154/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..83f1f441d4 --- /dev/null +++ b/challenge-154/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,23 @@ +use strict; +use warnings; +use feature "say"; + +my @permutations; +my %given_perm = map { $_ => 1 } qw/ + PELR PREL PERL PRLE PLER PLRE EPRL EPLR ERPL + ERLP ELPR ELRP RPEL RPLE REPL RELP RLPE RLEP + LPER LPRE LEPR LRPE LREP /; + +sub permute { + my ($str, @vals) = @_; + if (scalar @vals == 0) { + push @permutations, $str; + return; + } + permute("$str" . $vals[$_], @vals[0..$_-1], + @vals[$_+1..$#vals]) for 0..$#vals; +} +permute "", split //, (keys %given_perm)[0]; +for my $perm (@permutations) { + say "$perm is missing" unless exists $given_perm{$perm}; +} diff --git a/challenge-154/laurent-rosenfeld/perl/ch-2.pl b/challenge-154/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..4261ec9dc6 --- /dev/null +++ b/challenge-154/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,50 @@ +use strict; +use warnings; +use feature "say"; +use constant MAX => 10000; # MAX must be an even number + +my @primes = prime_list(MAX); +# say "@primes"; # 2 3 5 7 11 13 17 19 23 29 31 37 41 ... + +my @padovans = (1, 1, 1); +for my $i (3..140) { + $padovans[$i] = $padovans[$i-2] + $padovans[$i-3] +} +# say "@padovans"; # 1 1 1 2 2 3 4 5 7 9 12 16 21 28 37 ... + +my $count = 0; +my $last_pad = 0; +for my $pad (@padovans) { + next if $pad == $last_pad; + $last_pad = $pad; + next unless is_prime($pad); + say $pad; + $count++; + last if $count > 9; +} + +sub prime_list { + my $max = shift; + my @prime_list = (2, 3, 5); + PRIMES: for my $c (7..$max) { + for my $i (2..$c/2) { + next PRIMES unless $c % $i; + } + push @prime_list, $c; + } + return @prime_list; +} +sub is_prime { + my $num = shift; + for my $prime (@primes) { + return 1 if $num == $prime; + return 0 if $prime > $num; + return 0 unless $num % $prime; + } + my $test = MAX+1; + while ($test < int(sqrt($num))) { + return 0 unless $num % $test; + $test += 2; + } + return 1; +} diff --git a/challenge-154/laurent-rosenfeld/raku/ch-1.raku b/challenge-154/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..f378f0da4f --- /dev/null +++ b/challenge-154/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,7 @@ +my $given_perm = set(qw/ + PELR PREL PERL PRLE PLER PLRE EPRL EPLR ERPL + ERLP ELPR ELRP RPEL RPLE REPL RELP RLPE RLEP + LPER LPRE LEPR LRPE LREP /); +for "PERL".comb.permutations>>.join("") -> $perm { + say "$perm is missing" if $perm ∉ $given_perm; +} diff --git a/challenge-154/laurent-rosenfeld/raku/ch-1a.raku b/challenge-154/laurent-rosenfeld/raku/ch-1a.raku new file mode 100644 index 0000000000..6d1e50df30 --- /dev/null +++ b/challenge-154/laurent-rosenfeld/raku/ch-1a.raku @@ -0,0 +1,6 @@ +my $given_perm = set(qw/ + PELR PREL PERL EPRL EPLR ERPL + ERLP ELPR ELRP RPEL RLPE RLEP + LPER LPRE LEPR LRPE LREP /); +my $perms = set($given_perm.keys[0].comb.permutations>>.join("")); +say "Missing: ", ~($perms (-) $given_perm); diff --git a/challenge-154/laurent-rosenfeld/raku/ch-2.raku b/challenge-154/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..9ddb631bdf --- /dev/null +++ b/challenge-154/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,15 @@ +my @padovans = 1, 1, 1, -> $a, $b, $c { $a + $b } ... *; +# say @padovans[0..10]; # (1 1 1 2 2 3 4 5 7 9 12) +my $max = 10; +my $prev_pad = 1; +for @padovans -> $pad { + next if $prev_pad == $pad; + if $pad.is-prime { + print "$pad "; + $max--; + } + $prev_pad = $pad; + last if $max <= 0; +} +say ""; +say now - INIT now; |
