From f772985195011d1f0492b76d888183b17bcdca8d Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Sat, 8 Feb 2020 22:18:11 +0100 Subject: better explanation --- challenge-046/markus-holzer/raku/ch-2.p6 | 17 +++++++++++------ 1 file 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 -- cgit