757 Stealthy Numbers -- v2.pl 968 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 05 June 2021
  4. # https://github.com/trizen
  5. # Stealthy Numbers
  6. # https://projecteuler.net/problem=757
  7. # These are numbers of the form x*(x+1) * y*(y+1), where x and y are positive integers.
  8. # Also known as bipronic numbers.
  9. # See also:
  10. # https://oeis.org/A072389
  11. # Runtime: ~2 minutes.
  12. # WARNING: requires more than 4 GB of RAM!
  13. # See the Julia version for a faster solution.
  14. use 5.020;
  15. use strict;
  16. use warnings;
  17. use ntheory qw(:all);
  18. use experimental qw(signatures);
  19. sub p757 ($n) {
  20. my %seen;
  21. my $count = 0;
  22. my $limit = rootint($n, 4);
  23. foreach my $x (1 .. $limit) {
  24. for (my $y = $x ; ; ++$y) {
  25. my $v = $x * ($x + 1) * $y * ($y + 1);
  26. last if ($v > $n);
  27. if (not exists $seen{$v}) {
  28. undef $seen{$v};
  29. ++$count;
  30. }
  31. }
  32. }
  33. return $count;
  34. }
  35. say p757(powint(10, 6));
  36. say p757(powint(10, 14));