summaryrefslogtreecommitdiff
path: root/008/ch1.pl
blob: 81d590c2f3cf887abb44d249ee9b7fe0d981b144 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env perl

use strict;
use warnings;

my @primes;

sub perfect {
    my $sum = 0;
    my $n = shift;
    my $candidate = 1;
    while ($candidate*2 <= $n) {
        $sum += $candidate if $n % $candidate == 0;
        $candidate++;
    }

    return $n == $sum;
}

my $n = 1;
my $perfects = 0;
while ($perfects < 5) {
    if (perfect($n)) {
        print $n, "\n";
        $perfects++;
    }
    $n++;
}