aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaldhar H. Vyas <jaldhar@braincells.com>2021-12-27 01:20:54 -0500
committerJaldhar H. Vyas <jaldhar@braincells.com>2021-12-27 01:20:54 -0500
commit7639615d4115f8f6f073da82b7eede0de1d1f9d9 (patch)
tree9af8389a6961e89b6555faa2b7dd5bd1d56e5809
parent49bb22fa3a25f13e80466a5c7401997f3b194c1b (diff)
downloadperlweeklychallenge-club-7639615d4115f8f6f073da82b7eede0de1d1f9d9.tar.gz
perlweeklychallenge-club-7639615d4115f8f6f073da82b7eede0de1d1f9d9.tar.bz2
perlweeklychallenge-club-7639615d4115f8f6f073da82b7eede0de1d1f9d9.zip
Challenge 144 by Jaldhar H. Vyas.
-rw-r--r--challenge-144/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-144/jaldhar-h-vyas/perl/ch-1.pl58
-rwxr-xr-xchallenge-144/jaldhar-h-vyas/perl/ch-2.pl49
-rwxr-xr-xchallenge-144/jaldhar-h-vyas/raku/ch-1.raku12
-rwxr-xr-xchallenge-144/jaldhar-h-vyas/raku/ch-2.raku21
5 files changed, 141 insertions, 0 deletions
diff --git a/challenge-144/jaldhar-h-vyas/blog.txt b/challenge-144/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..d91e8e7631
--- /dev/null
+++ b/challenge-144/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2021/12/perl_weekly_challenge_week_144.html
diff --git a/challenge-144/jaldhar-h-vyas/perl/ch-1.pl b/challenge-144/jaldhar-h-vyas/perl/ch-1.pl
new file mode 100755
index 0000000000..717b188c9c
--- /dev/null
+++ b/challenge-144/jaldhar-h-vyas/perl/ch-1.pl
@@ -0,0 +1,58 @@
+#!/usr/bin/perl
+use 5.020;
+use warnings;
+
+sub combinations {
+ my @list = @{$_[0]};
+ my $length = $_[1];
+
+ if ($length <= 1) {
+ return map [$_], @list;
+ }
+
+ my @combos;
+
+ for (my $i = 0; $i + $length <= scalar @list; $i++) {
+ my $val = $list[$i];
+ my @rest = @list[$i + 1 .. $#list];
+ for my $c (combinations(\@rest, $length - 1)) {
+ push @combos, [$val, @{$c}] ;
+ }
+ }
+
+ return @combos;
+}
+
+sub isPrime {
+ my ($n) = @_;
+
+ if ($n < 2) {
+ return undef;
+ }
+
+ if ($n == 2) {
+ return 1;
+ }
+
+ for my $i (2 .. sqrt($n)) {
+ if ($n % $i == 0) {
+ return undef;
+ }
+ }
+
+ return 1;
+}
+
+my $n = shift // die "Need an integer.\n";
+
+my @factors = grep { $n % $_ == 0 && isPrime($_) } (2 .. $n / 2);
+if (scalar @factors == 1) {
+ say $factors[0] * $factors[0] == $n ? 1 : 0;
+} else {
+ say
+ scalar
+ (grep { $_ == $n }
+ (map { $_->[0] * $_->[1] }
+ combinations (\@factors, 2)))
+ ? 1 : 0;
+} \ No newline at end of file
diff --git a/challenge-144/jaldhar-h-vyas/perl/ch-2.pl b/challenge-144/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..0fe4866b04
--- /dev/null
+++ b/challenge-144/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+use 5.020;
+use warnings;
+
+sub combinations {
+ my @list = @{$_[0]};
+ my $length = $_[1];
+
+ if ($length <= 1) {
+ return map [$_], @list;
+ }
+
+ my @combos;
+
+ for (my $i = 0; $i + $length <= scalar @list; $i++) {
+ my $val = $list[$i];
+ my @rest = @list[$i + 1 .. $#list];
+ for my $c (combinations(\@rest, $length - 1)) {
+ push @combos, [$val, @{$c}] ;
+ }
+ }
+
+ return @combos;
+}
+
+if (scalar @ARGV != 2) {
+ die "Need two integers.\n";
+}
+
+my ($u, $v) = @ARGV;
+
+my @ulams = ($u, $v, $u + $v);
+
+while (scalar @ulams < 10) {
+ my @candidates = map { $_->[0] + $_->[1] } combinations(\@ulams, 2);
+
+
+ my %freq;
+ for my $candidate (@candidates) {
+ $freq{$candidate}++;
+ }
+ @candidates = grep { %freq{$_} < 2 } keys %freq;
+
+ @candidates = grep { $a = $_; !grep { $_ == $a } @ulams; } @candidates;
+
+ push @ulams, (sort { $a <=> $b} @candidates)[0];
+}
+
+say join q{, }, @ulams;
diff --git a/challenge-144/jaldhar-h-vyas/raku/ch-1.raku b/challenge-144/jaldhar-h-vyas/raku/ch-1.raku
new file mode 100755
index 0000000000..1b8f22b5ad
--- /dev/null
+++ b/challenge-144/jaldhar-h-vyas/raku/ch-1.raku
@@ -0,0 +1,12 @@
+#!/usr/bin/raku
+
+sub MAIN(Int $n) {
+ my @factors = (2 .. $n / 2).grep({ $n %% $_ && $_.is-prime });
+ if @factors.elems == 1 {
+ say @factors[0] * @factors[0] == $n ?? 1 !! 0;
+ } else {
+ say
+ @factors.combinations(2).map({ [*] $_; }).grep({ $_ == $n;}).elems
+ ?? 1 !! 0;
+ }
+}
diff --git a/challenge-144/jaldhar-h-vyas/raku/ch-2.raku b/challenge-144/jaldhar-h-vyas/raku/ch-2.raku
new file mode 100755
index 0000000000..1b6939ac84
--- /dev/null
+++ b/challenge-144/jaldhar-h-vyas/raku/ch-2.raku
@@ -0,0 +1,21 @@
+#!/usr/bin/raku
+
+sub MAIN(Int $u, Int $v) {
+ my @ulams = $u, $v, $u + $v;
+
+ while @ulams.elems < 10 {
+ @ulams.push(
+ @ulams
+ .combinations(2)
+ .map({ [+] $_; })
+ .classify( { $_ }, into => my %freq )
+ .keys
+ .map({ $_.Int; })
+ .grep({ %freq{$_}.elems < 2; })
+ .grep({ $_ ∉ @ulams; })
+ .min
+ );
+ }
+
+ @ulams.join(q{, }).say;
+} \ No newline at end of file