aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-03-02 03:37:50 +0000
committerGitHub <noreply@github.com>2021-03-02 03:37:50 +0000
commit623bf271d79193da3fe9bdd6db461055b055f4eb (patch)
tree5df86397f7d2c8028ddcf19b7fe723c08468f251
parent2a065f574cdd10f3a72fb7a6ae2484d43be77be9 (diff)
parentddc2ed0754e6cac1a7faf53bdec0bcb6b7a382e4 (diff)
downloadperlweeklychallenge-club-623bf271d79193da3fe9bdd6db461055b055f4eb.tar.gz
perlweeklychallenge-club-623bf271d79193da3fe9bdd6db461055b055f4eb.tar.bz2
perlweeklychallenge-club-623bf271d79193da3fe9bdd6db461055b055f4eb.zip
Merge pull request #3644 from jacoby/master
Challenge Accepted!
-rw-r--r--challenge-102/dave-jacoby/blog.txt1
-rw-r--r--challenge-102/dave-jacoby/perl/ch-1.pl44
-rw-r--r--challenge-102/dave-jacoby/perl/ch-2.pl26
3 files changed, 71 insertions, 0 deletions
diff --git a/challenge-102/dave-jacoby/blog.txt b/challenge-102/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..0461943e8b
--- /dev/null
+++ b/challenge-102/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby.github.io/2021/03/01/rare-numbers-and-hash-perl-challenge-102.html \ No newline at end of file
diff --git a/challenge-102/dave-jacoby/perl/ch-1.pl b/challenge-102/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..05d5ddad07
--- /dev/null
+++ b/challenge-102/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ postderef say signatures state };
+no warnings qw{ experimental };
+
+use Carp;
+
+my $i = shift @ARGV;
+$i //= 2;
+croak 'non-positive integer' if $i < 1;
+croak 'not an integer' if $i =~ /\D/;
+
+say join "\n\t", "RARE NUMBERS: $i", rare_numbers($i), '';
+
+# a number r is "rare" when:
+# - r1 is that number reversed
+# - sqrt(r+r1) is an integer
+# - sqrt(r-r1) is an integer
+sub rare_numbers ( $i ) {
+ my @output;
+
+ # given i == 5,
+ # low is 10000, the smallest five-digit number
+ # high is 99999, the highest five-digit number
+ my $low = join '', map { $_ == 1 ? 1 : 0 } 1 .. $i;
+ my $high = 9 x $i;
+
+ for my $r ( $low .. $high ) {
+ my $r1 = reverse $r;
+ next if $r - $r1 < 0; # early block for thing that break sqrt
+
+ my $s1 = sqrt( $r + $r1 );
+ next if $s1 =~ /\D/; # test if integer
+
+ my $s2 = sqrt( $r - $r1 );
+ next if $s2 =~ /\D/; # test if integer
+
+
+ push @output, $r;
+ }
+ return @output;
+}
diff --git a/challenge-102/dave-jacoby/perl/ch-2.pl b/challenge-102/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..630c256e61
--- /dev/null
+++ b/challenge-102/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ postderef say signatures state };
+no warnings qw{ experimental };
+
+use Carp;
+
+my $i = shift @ARGV;
+$i //= 2;
+croak 'non-positive integer' if $i < 1;
+croak 'not an integer' if $i =~ /\D/;
+
+say join "\t", $i, hash_count($i);
+
+sub hash_count( $i ) {
+ my $output = '';
+ my $j = $i;
+ while ( $j > 0 ) {
+ $output = '#' . $output;
+ $output = $j . $output if length $output < $i;
+ $j = $i - length $output;
+ }
+ return $output;
+}