Solutions to the first two tasks of the 20th Perl Weekly Challenge.
Write a script to accept a string from the command line and split it on change of character. For example, if the string is “ABBCDEEF”, then it should split like “A”, “BB”, “C”, “D”, “EE”, “F”.
say .comb(/(.)$0*/) for @*ARGS;
Write a script to print the smallest pair of Amicable Numbers. For more information, please checkout this Wikipedia page.
sub divisors($_) { grep $_ %% *, (1, 2 ...^ * * 2 > $_) }
my @amicable-numbers = (1..^Inf).map: -> $a {
my $b = $a.&divisors.sum;
$a, $b if $a != $b && $b.&divisors.sum == $a;
}
sub MAIN(UInt $N = 1) {
.say for @amicable-numbers[^$N];
}
The first pair is (220 284)
.