diff options
| author | Markus "Holli" Holzer <holli.holzer@gmail.com> | 2020-02-08 22:18:11 +0100 |
|---|---|---|
| committer | Markus "Holli" Holzer <holli.holzer@gmail.com> | 2020-02-08 22:18:11 +0100 |
| commit | f772985195011d1f0492b76d888183b17bcdca8d (patch) | |
| tree | ebfda6097138a9d7e603d5beb11d17e15930175b | |
| parent | 5245b91394c619ff405b3659346088620ccd32aa (diff) | |
| download | perlweeklychallenge-club-f772985195011d1f0492b76d888183b17bcdca8d.tar.gz perlweeklychallenge-club-f772985195011d1f0492b76d888183b17bcdca8d.tar.bz2 perlweeklychallenge-club-f772985195011d1f0492b76d888183b17bcdca8d.zip | |
better explanation
| -rw-r--r-- | challenge-046/markus-holzer/raku/ch-2.p6 | 17 |
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 |
