516 5-smooth totients.pl 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 04 May 2017
  5. # https://github.com/trizen
  6. # https://projecteuler.net/problem=516
  7. # Runtime: 41.831s.
  8. use 5.010;
  9. use strict;
  10. use ntheory qw(
  11. factor
  12. euler_phi
  13. is_prime
  14. );
  15. my @smooth = (1);
  16. my $limit = 1e12;
  17. my $mod = 1 << 32;
  18. foreach my $p (2, 3, 5) {
  19. foreach my $n (@smooth) {
  20. if ($n * $p <= $limit) {
  21. push @smooth, $n * $p;
  22. }
  23. }
  24. }
  25. @smooth =
  26. sort { $b <=> $a }
  27. grep { is_prime($_) }
  28. map { $_ + 1 } @smooth;
  29. my @h = (1);
  30. my $sum = 1;
  31. foreach my $p (@smooth) {
  32. foreach my $n (@h) {
  33. if (
  34. $p * $n <= $limit
  35. and (factor(euler_phi($n * $p)))[-1] <= 5
  36. ) {
  37. if ($p == 2) { # optional optimization
  38. foreach my $n (@h) {
  39. while ($n * $p <= $limit) {
  40. $sum += ($n * $p) % $mod;
  41. $sum %= $mod;
  42. $n *= $p;
  43. }
  44. }
  45. last;
  46. }
  47. else {
  48. $sum += ($n * $p) % $mod;
  49. $sum %= $mod;
  50. push @h, $n * $p;
  51. }
  52. }
  53. }
  54. say "$p -> $sum ($#h)";
  55. }
  56. say "Final answer: $sum";