aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-03-07 20:37:48 +0000
committerGitHub <noreply@github.com>2021-03-07 20:37:48 +0000
commite47f9302aa12df4f3b4c019140c36dcb2c2cec55 (patch)
tree83b0a62fb9d0e02e909ca68b7dc10dab35900528
parent55678f5860f0a6a40099f990473b741b864f93c4 (diff)
parent7f89801a5a02e8a4961176c8942ed5332faf5fe5 (diff)
downloadperlweeklychallenge-club-e47f9302aa12df4f3b4c019140c36dcb2c2cec55.tar.gz
perlweeklychallenge-club-e47f9302aa12df4f3b4c019140c36dcb2c2cec55.tar.bz2
perlweeklychallenge-club-e47f9302aa12df4f3b4c019140c36dcb2c2cec55.zip
Merge pull request #3679 from aaronreidsmith/challenge-102
Challenge 102 - Raku
-rw-r--r--challenge-102/aaronreidsmith/blog.txt1
-rw-r--r--challenge-102/aaronreidsmith/raku/ch-1.raku61
-rw-r--r--challenge-102/aaronreidsmith/raku/ch-2.raku37
3 files changed, 99 insertions, 0 deletions
diff --git a/challenge-102/aaronreidsmith/blog.txt b/challenge-102/aaronreidsmith/blog.txt
new file mode 100644
index 0000000000..1defd55470
--- /dev/null
+++ b/challenge-102/aaronreidsmith/blog.txt
@@ -0,0 +1 @@
+https://aaronreidsmith.github.io/blog/perl-weekly-challenge-102/
diff --git a/challenge-102/aaronreidsmith/raku/ch-1.raku b/challenge-102/aaronreidsmith/raku/ch-1.raku
new file mode 100644
index 0000000000..4f7bbf4ed6
--- /dev/null
+++ b/challenge-102/aaronreidsmith/raku/ch-1.raku
@@ -0,0 +1,61 @@
+#!/usr/bin/env raku
+
+sub digital-root(Int $N) returns Int {
+ my @digits = $N.comb;
+ my $digital-root = [+] @digits;
+ while @digits.elems > 1 {
+ @digits = $digital-root.comb;
+ $digital-root = [+] @digits;
+ }
+ $digital-root;
+}
+
+sub is-rare(Int $N) returns Bool {
+ return False if $N.comb.head % 2 != 0;
+ return False if digital-root($N) ~~ 0|1|3|4|6|7;
+
+ my $reversed = $N.flip.Int;
+ my $difference = $N - $reversed;
+
+ if $difference >= 0 && $difference.sqrt.narrow ~~ Int {
+ # Only calculate this if the difference is valid
+ my $sum = $N + $reversed;
+ $sum.sqrt.narrow ~~ Int;
+ } else {
+ False;
+ }
+}
+
+sub challenge(Int $N) returns Str {
+ my $min = ('2' ~ ('0' x $N - 1)).Int; # Rare number can never start with an odd digit.
+ my $max = ('8' ~ ('9' x $N - 1)).Int; # Rare number can never start with an odd digit.
+ ($min..$max).hyper.grep(&is-rare).join(', ');
+}
+
+multi sub MAIN(Int $N) {
+ say challenge($N);
+}
+
+multi sub MAIN(Bool :$test, Bool :$vebose = False) {
+ use Test;
+
+ my @tests = (
+ (1, '2, 8'),
+ (2, '65'),
+ (3, '242'),
+ (4, ''),
+ (5, '20402, 24642'),
+ (6, '621770')
+ );
+
+ for @tests -> ($N, $expected) {
+ is(challenge($N), $expected);
+ }
+
+ # This one is pretty slow, so it is not run by default
+ if $vebose {
+ is(challenge(9), '281089082');
+ }
+
+ done-testing;
+}
diff --git a/challenge-102/aaronreidsmith/raku/ch-2.raku b/challenge-102/aaronreidsmith/raku/ch-2.raku
new file mode 100644
index 0000000000..08b69b6822
--- /dev/null
+++ b/challenge-102/aaronreidsmith/raku/ch-2.raku
@@ -0,0 +1,37 @@
+#!/usr/bin/env raku
+
+sub challenge(Int $N) returns Str {
+ my @output;
+ my $index = $N - 1;
+ while $index >= 0 {
+ @output[$index] = '#';
+ my $position = $index + 1; # Position is 1-based while index is 0-based
+ for $position.flip.comb.kv -> $offset, $digit {
+ @output[$index - ($offset + 1)] = $digit;
+ }
+ $index -= ($position.chars + 1);
+ }
+ @output.join;
+}
+
+multi sub MAIN(Int $N) {
+ say challenge($N);
+}
+
+multi sub MAIN(Bool :$test) {
+ use Test;
+
+ my @tests = (
+ (1, '#'),
+ (2, '2#'),
+ (3, '#3#'),
+ (10, '#3#5#7#10#'),
+ (14, '2#4#6#8#11#14#')
+ );
+
+ for @tests -> ($N, $expected) {
+ is(challenge($N), $expected);
+ }
+
+ done-testing;
+}