634 Numbers of the form a^2b^3 -- v2.pl 927 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/perl
  2. # Numbers of the form a^2 * b^3, where a,b > 1.
  3. # https://projecteuler.net/problem=634
  4. # Using the method posted by al13n.
  5. # Runtime: 0.603s
  6. use 5.020;
  7. use strict;
  8. use warnings;
  9. use ntheory qw(:all);
  10. use experimental qw(signatures);
  11. sub my_powerfree_count ($n, $k = 2) {
  12. my $count = 0;
  13. forsquarefree {
  14. $count += ((scalar(@_) & 1) ? -1 : 1) * divint($n, powint($_, $k));
  15. } rootint($n, $k);
  16. return $count;
  17. }
  18. sub p_634 ($n) {
  19. my $count = 0;
  20. forsquarefree {
  21. $count += sqrtint(divint($n, $_*$_*$_)) - 1;
  22. } 2, rootint($n, 3);
  23. foreach my $k (2..rootint($n, 6)) {
  24. $count += powerfree_count(sqrtint(divint($n, powint($k, 6))), 3);
  25. }
  26. $count -= prime_count(rootint($n, 6));
  27. $count;
  28. }
  29. p_634(2 * 1e4) == 130 or die "error";
  30. p_634(3 * 1e6) == 2014 or die "error";
  31. p_634(5 * 1e9) == 91255 or die "error";
  32. say p_634(mulint(9, powint(10, 18)));