aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-03-02 04:43:03 +0000
committerGitHub <noreply@github.com>2021-03-02 04:43:03 +0000
commitef84b0329e208c18386ca59e8c8793331955db37 (patch)
tree097badfeab081bf372fd6f14f95efa71883eb7b1
parent8923f4e8d2a04a1df2d52e199bae59f776ee9a93 (diff)
parent3545523b3d7228b91d928f621932750e61d6430d (diff)
downloadperlweeklychallenge-club-ef84b0329e208c18386ca59e8c8793331955db37.tar.gz
perlweeklychallenge-club-ef84b0329e208c18386ca59e8c8793331955db37.tar.bz2
perlweeklychallenge-club-ef84b0329e208c18386ca59e8c8793331955db37.zip
Merge pull request #3646 from gnustavo/102
Add Gustavo Chaves's solutions to challenge 102
-rwxr-xr-xchallenge-102/gustavo-chaves/perl/ch-1.pl31
-rwxr-xr-xchallenge-102/gustavo-chaves/perl/ch-2.pl19
2 files changed, 50 insertions, 0 deletions
diff --git a/challenge-102/gustavo-chaves/perl/ch-1.pl b/challenge-102/gustavo-chaves/perl/ch-1.pl
new file mode 100755
index 0000000000..65c17408a6
--- /dev/null
+++ b/challenge-102/gustavo-chaves/perl/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-102/
+# TASK #1 › Rare Numbers
+
+use 5.030;
+use warnings;
+
+my $digits = shift or die "Usage: $0 N\n";
+
+my @squares = (0);
+while ($squares[-1] < 2*(10**$digits)) {
+ push @squares, @squares * @squares;
+}
+
+# This is very inefficient... :-(
+
+for (my $r = $#squares; $r > 1; --$r) {
+ my $xx = $squares[$r];
+ for (my $l = $r-1; $l >= 1; --$l) {
+ my $yy = $squares[$l];
+ my $dr = $xx + $yy;
+ next unless ($dr % 2) == 0;
+ my $dl = $xx - $yy;
+ next unless ($dl % 2) == 0;
+ my $R = $dr / 2;
+ next unless length("$R") == $digits;
+ my $L = sprintf "%0${digits}d", $dl / 2;
+ say $R if "$R" eq reverse("$L");
+ }
+}
diff --git a/challenge-102/gustavo-chaves/perl/ch-2.pl b/challenge-102/gustavo-chaves/perl/ch-2.pl
new file mode 100755
index 0000000000..d7a13c1267
--- /dev/null
+++ b/challenge-102/gustavo-chaves/perl/ch-2.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-102/
+# TASK #2 › Hash-counting String
+
+use 5.030;
+use warnings;
+
+my $N = shift or die "Usage: $0 N\n";
+
+my $string = '#';
+
+while ($N > 1) {
+ $string = "$N$string";
+ $N -= 1 + length "$N";
+ $string = "#$string" if $N > 0;
+}
+
+say $string;