diff options
| author | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2020-02-21 16:26:46 +0100 |
|---|---|---|
| committer | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2020-02-21 16:26:46 +0100 |
| commit | e9180aa8f40efa262470c85fd18d2c49853b72f3 (patch) | |
| tree | fd6e601dad2fcd339bedffed64875cf8b69eda3c /challenge-048 | |
| parent | 2655e98848fd80f7c9cc3442cc2b5596a2d17854 (diff) | |
| download | perlweeklychallenge-club-e9180aa8f40efa262470c85fd18d2c49853b72f3.tar.gz perlweeklychallenge-club-e9180aa8f40efa262470c85fd18d2c49853b72f3.tar.bz2 perlweeklychallenge-club-e9180aa8f40efa262470c85fd18d2c49853b72f3.zip | |
Solution to challenge 048 task 1 and 2 in Raku by Noud
Diffstat (limited to 'challenge-048')
| -rw-r--r-- | challenge-048/noud/raku/ch-1.p6 | 34 | ||||
| -rw-r--r-- | challenge-048/noud/raku/ch-2.p6 | 22 |
2 files changed, 56 insertions, 0 deletions
diff --git a/challenge-048/noud/raku/ch-1.p6 b/challenge-048/noud/raku/ch-1.p6 new file mode 100644 index 0000000000..6afe800033 --- /dev/null +++ b/challenge-048/noud/raku/ch-1.p6 @@ -0,0 +1,34 @@ +# Survivor +# +# There are 50 people standing in a circle in position 1 to 50. The person +# standing at position 1 has a sword. He kills the next person i.e. standing at +# position 2 and pass on the sword to the immediate next i.e. person standing +# at position 3. Now the person at position 3 does the same and it goes on +# until only one survives. +# +# Write a script to find out the survivor. + +# This is a special case of the Josephus problem: +# +# https://en.wikipedia.org/wiki/Josephus_problem +# and +# +# https://oeis.org/A006257 +# +# Let n be the total number of people in the circle and k be the step to the +# next person. I.e. k - 2 people are skipped, person k is killed and person +# k will get the sword. Let f(n, k) denote the position of the survivor. If +# person k is killed, we're left with a circle of n - 1 and the next person who +# gets the sword is (k mod n) + 1. The survivor person in the remaining circle +# is f(n - 1, k) if we start counting at 1. Shifting the numbers the survivor +# person is (f(n - 1, k) + k - 1) mod n + 1. Hence we have the recurrence +# relation: +# +# f(n, k) = (f(n - 1, k) + k - 1) mod n + 1. +# +# For this problem we have n = 50 and k = 2. + +multi sub f(1, $k) { 1; } +multi sub f($n, $k) { (f($n - 1, $k) + $k - 1) % $n + 1; } + +say "Survivor: ", f(50, 2); diff --git a/challenge-048/noud/raku/ch-2.p6 b/challenge-048/noud/raku/ch-2.p6 new file mode 100644 index 0000000000..9affb72031 --- /dev/null +++ b/challenge-048/noud/raku/ch-2.p6 @@ -0,0 +1,22 @@ +# Palindrome Dates +# +# Write a script to print all Palindrome Dates between 2000 and 2999. The +# format of date is mmddyyyy. For example, the first one was on October 2, 2001 +# as it is represented as 10022001. + +# It's possible to compute directly all possibilities. Let +# "m1 m2 d1 d2 y1 y2 y3 y4" be the palindrome, because we look between years 2000 +# and 2999, y1 = 2. Because we have a palindrome: +# +# "m1 m2 d1 d2 2 y2 y3 y4" = "y4 y3 y2 2 d2 d1 m2 m1" +# +# Hence y4 = m1, y3 = m2, y2 = d1, and d2 = 2. The palindrome is of the shape +# +# "m1 m2 d1 2 2 d1 m2 m1" +# +# The only days possible with d2 = 2 are 02, 12, and 22, hence d1 = 0, 1 or 2. +# For m1 and m2 we have 12 months left. + +for 1..12 X ^3 -> ($m, $d) { + say ($m div 10), ($m % 10), $d, 2, 2, $d, ($m % 10), ($m div 10); +} |
