023 Non-abundant sums.pl 664 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # https://github.com/trizen
  4. # Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
  5. # https://projecteuler.net/problem=23
  6. # Runtime: 4.463s
  7. use 5.010;
  8. use integer;
  9. use ntheory qw(divisor_sum);
  10. my @abundants;
  11. foreach my $i (1 .. 28123) {
  12. if (divisor_sum($i) - $i > $i) {
  13. push @abundants, $i;
  14. }
  15. }
  16. my %seen;
  17. foreach my $i (@abundants) {
  18. foreach my $j (@abundants) {
  19. last if $i + $j > 28123;
  20. undef $seen{$i + $j};
  21. }
  22. }
  23. my $sum = 0;
  24. foreach my $i (1 .. 28123) {
  25. if (not exists $seen{$i}) {
  26. $sum += $i;
  27. }
  28. }
  29. say $sum;