-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_23.pl
50 lines (41 loc) · 867 Bytes
/
problem_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
41
42
43
44
45
46
47
48
49
50
use strict;
use warnings;
use Memoize;
sub mu {
my $n = shift;
return 1 if $n == 1;
for( my $i = 2; $i <= $n; $i++ ) {
if( $n % $i == 0 ) {
return 0 if( ( $n / $i ) % $i == 0 );
return (-1) * mu( $n / $i );
}
}
}
sub dividers_sum {
my $n = shift;
return 0 if $n == 1;
my $sum = 0;
for( my $i = 2; $i <= $n; $i++ ) {
if( $n % $i == 0 ) {
$sum -= mu( $i ) * ( dividers_sum( $n / $i ) + $n / $i );
}
}
return $sum;
}
memoize( 'mu' );
memoize( 'dividers_sum' );
my @abundant;
foreach (1..28123) {
push @abundant, $_ if dividers_sum( $_ ) > $_;
}
my @nums;
foreach my $i (@abundant) {
foreach my $j (@abundant) {
$nums[$i+$j] = 1;
}
}
my $sum = 0;
foreach (1..28123) {
$sum += $_ if !defined $nums[ $_ ];
}
print "Answer: $sum\n";