use warnings; use strict; use Math::Prime::Util::GMP qw/sigma/; use Path::Class qw/file/; #OR: use Storable qw/store retrieve/; my $cache_file = '/tmp/abundant_sums_cache'; my @arr_sum_two_abundant_numbers; if (-e $cache_file) { @arr_sum_two_abundant_numbers = file($cache_file)->slurp(chomp=>1); #OR: @arr_sum_two_abundant_numbers = @{ retrieve($cache_file) }; } if (!@arr_sum_two_abundant_numbers) { print "regenerating \@arr_sum_two_abundant_numbers...\n"; my @arr_abundant = grep { sigma($_)>2*$_ } 12 .. 28123; my %hash; foreach my $i (@arr_abundant) { foreach my $j (@arr_abundant) { my $positive = $i + $j; next if $positive > 28123; $hash{$positive}++; } } @arr_sum_two_abundant_numbers = sort {$a <=> $b} keys %hash; file($cache_file)->spew_lines(\@arr_sum_two_abundant_numbers); #OR: store(\@arr_sum_two_abundant_numbers, $cache_file); } else { print "loaded \@arr_sum_two_abundant_numbers from $cache_file\n" } my $sum_total; my $n = 1; foreach my $key (@arr_sum_two_abundant_numbers) { for ( my $i=$n ; $i < $key ; $i++) { $sum_total += $i; } $n = $key +1; } print "$sum_total\n";