-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path23.pl
40 lines (34 loc) · 835 Bytes
/
23.pl
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
33
34
35
36
37
38
39
40
#!/usr/bin/perl
use strict;
use warnings;
use List::Util qw(sum);
my $upperLimit = 28123;
my %abundants;
my %abunSums;
for my $i (1..$upperLimit) {
if (isAbundant($i)) {
$abundants{$i} = 1;
}
}
for my $ab1 (keys %abundants) {
for my $ab2 (grep { $_ + $ab1 <= $upperLimit } keys %abundants) {
$abunSums{($ab1+$ab2)} = 1;
}
}
my $sum = 0;
for my $num (1..($upperLimit)) {
if (!$abunSums{$num}) { $sum += $num; }
}
print $sum;
sub isAbundant {
my $number = shift;
return $number < getSumDivs($number);
}
sub getSumDivs {
my $number = shift;
return 1 if $number == 1;
my @divs = grep { $number % $_ == 0 } 1 .. sqrt($number);
push @divs, , map {$number == $_*$_ ? () : $number/$_} reverse @divs;
pop(@divs); # they do not count self as a divisor
return sum(@divs);
}