Home > Raku > Pushing …

Pushing …

Thanks to , PWC 154 part 1 is so boring, I’m not going into details. Part two however is a bit of a head scratcher. We are asked to implement a sequence with the properties:

P(0) = P(1) = P(2) = 1
P(n) = P(n-2) + P(n-3)

Using the sequence operator requires a trick thought.

my \padovan = 1, 1, 1, * + * + 0 * * … ∞;
my \padovan-prime = padovan.grep(*.is-prime).unique;

I have to convince it to operate on the 1th and 2nd element of the initial list and then advance by one. For once, math is on my side because with 0 * * I can force a 3rd automatic variable into existence, without having it take part in building the sum. The rest is just filtering out anything that isn’t prime.

There is another way to skip arguments if we are willing to use a pointy-block.

my \padovan = 1, 1, 1, -> $a, $b, $ { $a + $b } … ∞;

This is not as clever but looks intentional. It’s probably better that way.

Categories: Raku