aboutsummaryrefslogtreecommitdiff
path: root/challenge-046
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-02-09 06:05:56 +0000
committerGitHub <noreply@github.com>2020-02-09 06:05:56 +0000
commitcc7bfcadd278f42c880cbccf5aba1302b1947d5e (patch)
tree0c0befa0b4aa20ba32aaa7742fb106a4014d7fe8 /challenge-046
parentcb79df58493c0e127df8796e80df4110051bb513 (diff)
parentf772985195011d1f0492b76d888183b17bcdca8d (diff)
downloadperlweeklychallenge-club-cc7bfcadd278f42c880cbccf5aba1302b1947d5e.tar.gz
perlweeklychallenge-club-cc7bfcadd278f42c880cbccf5aba1302b1947d5e.tar.bz2
perlweeklychallenge-club-cc7bfcadd278f42c880cbccf5aba1302b1947d5e.zip
Merge pull request #1226 from holli-holzer/master
I know. I promised.
Diffstat (limited to 'challenge-046')
-rw-r--r--challenge-046/markus-holzer/raku/ch-2.p617
1 files changed, 11 insertions, 6 deletions
diff --git a/challenge-046/markus-holzer/raku/ch-2.p6 b/challenge-046/markus-holzer/raku/ch-2.p6
index c804e864ab..13c3109c25 100644
--- a/challenge-046/markus-holzer/raku/ch-2.p6
+++ b/challenge-046/markus-holzer/raku/ch-2.p6
@@ -11,14 +11,19 @@
#
# In the case of a square number however, there is always one pair for which both elements are the same.
# 16 for example, has the divisor pairs are (1,16), (2, 8) and (4,4).
-# This last pair contains the same number twice.
+# The last (square) pair contains the same number twice.
# And that is what makes the total number of divisors odd.
-# And then that is what tells us the door 16 will be open.
+# And then that is what tells us the door 16, and all other squares, will be open.
#
-# Thus we can solve by
+# Thus we can know wether a door is open by checking if the room number is sqare.
-say "Open rooms: ", join ',', ( 1..500 ).grep: *.&is-open;
+say "Open rooms: ", ( 1..500 ).grep: *.&is-open;
+sub is-open( $room ) { $room.sqrt.narrow ~~ Int }
-sub is-open( $room ) { ($_ = $room.sqrt) && $_ == $_.Int; }
+# We could also simply generate the list of squares
-# Open rooms: 1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400,441,484 \ No newline at end of file
+say "Open rooms: ", (1..500.sqrt.Int).map: * ** 2;
+
+# Or even
+
+say "Open rooms: ", (1..500.sqrt.Int)>>²; # nicest idiom by jnthn \ No newline at end of file