aboutsummaryrefslogtreecommitdiff
path: root/challenge-020/noud/perl6/ch-2.p6
blob: 43e846f267284747bd634f162f878723e099edc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Write a script to print the smallest pair of Amicable Numbers. For more
# information, please checkout wikipedia page.


sub prop_div(Int $i) {
    return [+] (1..^$i).grep($i %% *);
}

# Anonymous cache handler. Trick copied from fjwhittle (ch-017, ch-1.p6).
&prop_div.wrap: -> $i { .[$i] //= callsame } given Array.new;


# Create a lazy list of all Amicable Numbers.
my @amicables = lazy gather for 1..^Inf -> $m {
    for 1..^$m -> $n {
        take ($n, $m) if prop_div($m) == $n and prop_div($n) == $m;
    }
}


# Print the first three Amicable Numbers.
$_.say for @amicables[^3];