023 Non-abundant sums -- v2.pl 722 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 07 November 2019
  4. # https://github.com/trizen
  5. # Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
  6. # https://projecteuler.net/problem=23
  7. # Runtime: 0.524s
  8. use 5.010;
  9. use integer;
  10. use ntheory qw(divisor_sum);
  11. use experimental qw(signatures);
  12. sub is_abundant ($n) {
  13. (divisor_sum($n) >> 1) > $n;
  14. }
  15. my $total = 0;
  16. my @abundant;
  17. my %abundant;
  18. OUTER: foreach my $n (1 .. 28123) {
  19. if (is_abundant($n)) {
  20. $abundant{$n} = 1;
  21. push @abundant, $n;
  22. }
  23. foreach my $k (@abundant) {
  24. if (exists $abundant{$n - $k}) {
  25. next OUTER;
  26. }
  27. }
  28. $total += $n;
  29. }
  30. say $total;