From b6990dba45b40936533a7a21d8658a0aca9bf817 Mon Sep 17 00:00:00 2001 From: Util Date: Sun, 3 Jul 2022 11:37:23 -0500 Subject: Add TWC 171 solutions by Bruce Gray : only Raku. --- challenge-171/bruce-gray/raku/ch-1.raku | 11 +++++++++++ challenge-171/bruce-gray/raku/ch-2.raku | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 challenge-171/bruce-gray/raku/ch-1.raku create mode 100644 challenge-171/bruce-gray/raku/ch-2.raku diff --git a/challenge-171/bruce-gray/raku/ch-1.raku b/challenge-171/bruce-gray/raku/ch-1.raku new file mode 100644 index 0000000000..e11ecea897 --- /dev/null +++ b/challenge-171/bruce-gray/raku/ch-1.raku @@ -0,0 +1,11 @@ +use Prime::Factor; + +constant @abundant_odd = grep { .&proper-divisors.sum > $_ }, (1, 3, 5 … Inf); + +say @abundant_odd.head(20); + + + +# https://oeis.org/A005231 +constant @A005231 = 945, 1575, 2205, 2835, 3465, 4095, 4725, 5355, 5775, 5985, 6435, 6615, 6825, 7245, 7425, 7875, 8085, 8415, 8505, 8925, 9135, 9555, 9765, 10395, 11025, 11655, 12285, 12705, 12915, 13545, 14175, 14805, 15015, 15435, 16065, 16695, 17325, 17955; +warn unless @abundant_odd.head(+@A005231) eqv @A005231.Seq; diff --git a/challenge-171/bruce-gray/raku/ch-2.raku b/challenge-171/bruce-gray/raku/ch-2.raku new file mode 100644 index 0000000000..dae9ab8216 --- /dev/null +++ b/challenge-171/bruce-gray/raku/ch-2.raku @@ -0,0 +1,21 @@ +my &compose := &[∘]; # This single binding is the complete solution to the task. + + +# Testing: +my &h = compose &ffff, &gggg; + +sub ffff ($p) { say "Calculating {&?ROUTINE.name} of $p"; $p * 2 } +sub gggg ($p) { say "Calculating {&?ROUTINE.name} of $p"; $p / 3 } + +say h(15); + +# Function composition is already provided by Raku's ∘ infix operator. +# See: https://docs.raku.org/language/operators#infix_o,_infix_%E2%88%98 + +# Of interest: The creation of &h can occur *before* the definition of the subs that will compose it. + +# TIMTOWTDI; any one of these lines could replace the first line: +# sub compose ( &f, &g --> Code ) { return &f ∘ &g } +# my &compose = -> &f, &g { &f ∘ &g }; +# my &compose = { &^f ∘ &^g }; +# my &compose = * ∘ *; -- cgit