This post is part of a series on Mohammad Anwar’s excellent Perl Weekly Challenge, where Perl and Raku hackers submit solutions to two different challenges every week. (It’s a lot of fun, if you’re into that sort of thing.)

Task 2 this week has us print the first 20 “gapful numbers,” as described by OEIS sequence A108343. Gapful numbers are numbers greater than 99 that are evenly divisible by their first and last digits combined. For example, 132 is a gapful number because 132 ÷ 12 = 11.

This is certainly the easier of the two tasks, as both Perl and Raku have convenient ways to index and concatenate string fragments.

Perl

In Perl, we can check if a number is gapful by split ting the number and join ing the first and last characters:

sub is_gapful(_) { $_ = pop; not $_ % join '', (split '')[0,-1] }

The modulus operation ( % ) will return 0 if the number is gapful, so we invert the result. The is_gapful(_) prototype will allow us to use is_gapful without arguments if we want to operate on $_ :

sub first_n_gapful { my $N = shift; my @r; for ($_ = 100; @r < $N; $_++) { push @r, $_ if is_gapful; } @r; }

I of course could have done without the prototype and used is_gapful($_) , but this reads a little bit better.

Raku

Raku makes this ridiculously easy. The is-gapful sub is similar:

sub is-gapful( Int

) { n ≥ 100 and n %% n.comb[0,*-1].join }

Note the %% divisibility operator makes the logic a little bit cleaner. We can make a lazy list of every gapful number:

my @gapful = (100..∞).grep: ＆is-gapful; [1]

Then, printing the first 20 looks like this:

say @gapful[^20];