From e2e4a57c3e76e6131a7b49cd35cfb1f17b34ccbd Mon Sep 17 00:00:00 2001 From: Flavio Poletti Date: Wed, 6 Jul 2022 22:54:51 +0200 Subject: Add polettix's solution to challenge-172 --- challenge-172/polettix/blog.txt | 1 + challenge-172/polettix/blog1.txt | 1 + challenge-172/polettix/perl/ch-1.pl | 70 +++++++++++++++++++++++++++++++++++ challenge-172/polettix/perl/ch-2.pl | 29 +++++++++++++++ challenge-172/polettix/raku/ch-1.raku | 12 ++++++ challenge-172/polettix/raku/ch-2.raku | 27 ++++++++++++++ 6 files changed, 140 insertions(+) create mode 100644 challenge-172/polettix/blog.txt create mode 100644 challenge-172/polettix/blog1.txt create mode 100644 challenge-172/polettix/perl/ch-1.pl create mode 100644 challenge-172/polettix/perl/ch-2.pl create mode 100644 challenge-172/polettix/raku/ch-1.raku create mode 100644 challenge-172/polettix/raku/ch-2.raku diff --git a/challenge-172/polettix/blog.txt b/challenge-172/polettix/blog.txt new file mode 100644 index 0000000000..405bfff3f5 --- /dev/null +++ b/challenge-172/polettix/blog.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2022/07/06/pwc172-prime-partition/ diff --git a/challenge-172/polettix/blog1.txt b/challenge-172/polettix/blog1.txt new file mode 100644 index 0000000000..63451e15fb --- /dev/null +++ b/challenge-172/polettix/blog1.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2022/07/07/pwc172-five-number-summary/ diff --git a/challenge-172/polettix/perl/ch-1.pl b/challenge-172/polettix/perl/ch-1.pl new file mode 100644 index 0000000000..d2cb1ad3ec --- /dev/null +++ b/challenge-172/polettix/perl/ch-1.pl @@ -0,0 +1,70 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +no warnings 'experimental::signatures'; +use List::Util 'sum'; + +my $n = shift // 18; +my $m = shift // 2; +say simple_stringify_array(prime_partition($n, $m)); + +sub simple_stringify_array (@a) { return '(' . join(', ', @a), ')' } + +sub prime_partition ($n, $m) { + if ($m == 1) { return is_prime($n) ? $n : () } + my $cit = combinations_iterator($m, primes_within(2, $n - 2)); + while (my ($c) = $cit->()) { + return $c->@* if $n == sum $c->@*; + } + return; +} + +sub combinations_iterator ($k, @items) { + my @indexes = (0 .. ($k - 1)); + my $n = @items; + return sub { + return unless @indexes; + my (@combination, @remaining); + my $j = 0; + for my $i (0 .. ($n - 1)) { + if ($j < $k && $i == $indexes[$j]) { + push @combination, $items[$i]; + ++$j; + } + else { + push @remaining, $items[$i]; + } + } + for my $incc (reverse(-1, 0 .. ($k - 1))) { + if ($incc < 0) { + @indexes = (); # finished! + } + elsif ((my $v = $indexes[$incc]) < $incc - $k + $n) { + $indexes[$_] = ++$v for $incc .. ($k - 1); + last; + } + } + return (\@combination, \@remaining); + } +} + +sub primes_within ($min, $max) { + my @retval = $min < 3 ? 2 : (); + $min++ unless $min % 2; + while ($min <= $max) { + push @retval, $min if is_prime($min); + $min += 2; + } + return @retval; +} + +sub is_prime { # https://en.wikipedia.org/wiki/Primality_test + return if $_[0] < 2; + return 1 if $_[0] <= 3; + return unless ($_[0] % 2) && ($_[0] % 3); + for (my $i = 6 - 1; $i * $i <= $_[0]; $i += 6) { + return unless ($_[0] % $i) && ($_[0] % ($i + 2)); + } + return 1; +} diff --git a/challenge-172/polettix/perl/ch-2.pl b/challenge-172/polettix/perl/ch-2.pl new file mode 100644 index 0000000000..62785c29b1 --- /dev/null +++ b/challenge-172/polettix/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +no warnings 'experimental::signatures'; + +my @values = (0, 0, 1, 2, 63, 61, 27, 13); +my @fives = five_number_summary(@values); +say "(@fives)"; + +sub five_number_summary (@input) { + state $emedian = sub ($aref, $from, $to) { + my $twom = $from + $to; + my $rem = $twom % 2; + my $lo = ($twom - $rem) / 2; + my $hi = $lo + 1; + my $medn = $rem ? ($aref->[$lo] + $aref->[$hi]) / 2 : $aref->[$lo]; + + # https://en.wikipedia.org/wiki/Quartile - Tukey's hinges + return ($medn, $rem ? ($lo, $hi) : ($lo, $lo)); + }; + @input = sort { $a <=> $b } @input; + + my ($median, $lo, $hi) = $emedian->(\@input, 0, $#input); + my ($lop) = $emedian->(\@input, 0, $lo); + my ($hip) = $emedian->(\@input, $hi, $#input); + + return ($input[0], $lop, $median, $hip, $input[-1]); +} diff --git a/challenge-172/polettix/raku/ch-1.raku b/challenge-172/polettix/raku/ch-1.raku new file mode 100644 index 0000000000..7bf4b0cef6 --- /dev/null +++ b/challenge-172/polettix/raku/ch-1.raku @@ -0,0 +1,12 @@ +#!/usr/bin/env raku +use v6; +sub MAIN ($n = 18, $m = 2) { say prime-partition($n, $m) } + +multi sub prime-partition ($n where *.is-prime, 1) { [ $n ] } +multi sub prime-partition ($n, 1) { [] } +multi sub prime-partition ($n, $m) { + for (2 .. $n - 2).grep({.is-prime}).combinations($m) -> $c { + return $c.Array if $c.sum == $n; + } + return []; +} diff --git a/challenge-172/polettix/raku/ch-2.raku b/challenge-172/polettix/raku/ch-2.raku new file mode 100644 index 0000000000..8249be70a4 --- /dev/null +++ b/challenge-172/polettix/raku/ch-2.raku @@ -0,0 +1,27 @@ +#!/usr/bin/env raku +use v6; +sub MAIN { + my @values = (0, 0, 1, 2, 63, 61, 27, 13); + my @fives = five-number-summary(@values); + say @fives; +} + +sub five-number-summary (*@input) { + sub emedian ($from, $to) { + my $twom = $from + $to; + my $rem = $twom % 2; + my $lo = ($twom - $rem) / 2; + my $hi = $lo + 1; + my $medn = $rem ?? (@input[$lo] + @input[$hi]) / 2 !! @input[$lo]; + + # https://en.wikipedia.org/wiki/Quartile - Tukey's hinges + return [$medn, $rem ?? |($lo, $hi) !! |($lo, $lo)]; + } + @input = @input.sort: { $^a <=> $^b }; + + my ($median, $lo, $hi) = emedian(0, @input.end); + my ($lop) = emedian(0, $lo); + my ($hip) = emedian($hi, @input.end); + + return [@input[0], $lop, $median, $hip, @input[*-1]]; +} -- cgit