blob: bbd18935f86cd3e96fcc1193eeafa3ab2bf484e4 (
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
29
30
31
32
|
#!/usr/bin/env perl
#
# Write a script that computes the first five perfect numbers. A perfect number
# is an integer that is the sum of its positive proper divisors (all divisors
# except itself).
# (https://en.wikipedia.org/wiki/Perfect_number)
################################################################################
use strict;
use warnings;
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++;
}
|