diff options
| -rwxr-xr-x | challenge-102/gustavo-chaves/perl/ch-1.pl | 31 | ||||
| -rwxr-xr-x | challenge-102/gustavo-chaves/perl/ch-2.pl | 19 |
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; |
