aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-12-14 13:30:03 +0000
committerGitHub <noreply@github.com>2021-12-14 13:30:03 +0000
commit8ff36dc93ae4052e5631bfd824726bc88b5ed6c2 (patch)
treeace20d33abf37c291fcb3c4989f0cb30f3a39c63
parent4e2605928242b84ff96cbc8011c208daf1ce0020 (diff)
parent7172edadbb0b471cdc3570098fe3f9e3160699e6 (diff)
downloadperlweeklychallenge-club-8ff36dc93ae4052e5631bfd824726bc88b5ed6c2.tar.gz
perlweeklychallenge-club-8ff36dc93ae4052e5631bfd824726bc88b5ed6c2.tar.bz2
perlweeklychallenge-club-8ff36dc93ae4052e5631bfd824726bc88b5ed6c2.zip
Merge pull request #5375 from ccntrq/challenge-143
Challenge 143
-rwxr-xr-xchallenge-143/alexander-pankoff/perl/ch-1.pl7
-rwxr-xr-xchallenge-143/alexander-pankoff/perl/ch-2.pl61
2 files changed, 68 insertions, 0 deletions
diff --git a/challenge-143/alexander-pankoff/perl/ch-1.pl b/challenge-143/alexander-pankoff/perl/ch-1.pl
new file mode 100755
index 0000000000..fcb9c8edbc
--- /dev/null
+++ b/challenge-143/alexander-pankoff/perl/ch-1.pl
@@ -0,0 +1,7 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+my $expr = $ARGV[0];
+die "invalid expr\n" unless $expr && $expr =~ m/^[\s\d\.\+\-\*\(\)]*$/;
+print eval($expr) . "\n";
diff --git a/challenge-143/alexander-pankoff/perl/ch-2.pl b/challenge-143/alexander-pankoff/perl/ch-2.pl
new file mode 100755
index 0000000000..a952866cde
--- /dev/null
+++ b/challenge-143/alexander-pankoff/perl/ch-2.pl
@@ -0,0 +1,61 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature qw'say signatures';
+no warnings qw'experimental::signatures';
+
+use List::Util qw'first';
+
+use constant DEBUG => $ENV{DEBUG} // 0;
+
+run() unless caller();
+
+sub run() {
+ my $stealthy = stealthy( $ARGV[0] );
+ say $stealthy ? 1 : 0;
+ explain($stealthy) if DEBUG;
+}
+
+sub stealthy($n) {
+ my @divisors = find_divisors_pairs($n);
+ my @pairs = pairs(@divisors);
+
+ my $stealthy = first {
+ my ( $a, $b, $c, $d ) = flatten($_);
+ $a * $b == $c * $d && $a + $b == $c + $d + 1;
+ }
+ @pairs;
+
+ return $stealthy;
+}
+
+sub explain($stealthy) {
+ say "Not stealthy" && return if !$stealthy;
+ my ( $a, $b, $c, $d ) = flatten($stealthy);
+ say
+"Since $a (a) * $b (b) = $c (c) * $d (d) and $a (a) + $b (b) = $c (c) + $d (d) + 1";
+}
+
+sub pairs(@xs) {
+ my @out;
+ for my $i ( 0 .. $#xs - 1 ) {
+ push @out, map { [ $xs[$i], $xs[$_] ] } ( $i + 1 .. $#xs );
+ }
+ return @out;
+}
+
+sub flatten($xs) {
+ map { @$_ } @$xs;
+}
+
+sub find_divisors_pairs($x) {
+ my @out;
+ my $max = sqrt($x);
+ for my $i ( 1 .. $max ) {
+ if ( $x % $i == 0 ) {
+ push @out, [ $i, $x / $i ];
+ }
+ }
+
+ return @out;
+}