This post is part of a series on Mohammad Anwar’s excellent Weekly Challenge, where hackers submit solutions in Perl, Raku, or any other language, to two different challenges every week. (It’s a lot of fun, if you’re into that sort of thing.)
This week, Mohammad asks us to output a list of all Attractive Numbers between 1 and 50. Attractive numbers, as described by the Online Encyclopedia of Integer Sequences (OEIS) are:
Numbers with a prime number of prime divisors (counted with multiplicity)
OEIS Sequence A063989
The first numbers are 4, 6, 8, 9, 10, 12, 14, 15, 18, 20, 21, 22, 25, 26, 27, 28, 30, 32, 33, 34, 35, 38, 39, 42, 44, 45, 46, 48, 49, 50, …
This is a straightforward problem, but I’m not going to let that stop me from finding an interesting way to solve it. If I were looking for a sensible way to solve it, I’d do something like this:
use Math::Prime::Util ':all';
say for grep { is_prime( factor($_) ) } 1..50;