smooth_generate_almost_primes.pl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/perl
  2. # Least k such that phi(k) is an n-th power when k is the product of n distinct primes.
  3. # https://oeis.org/A281069
  4. # Upper-bounds:
  5. # a(11) <= 5703406198237930
  6. # a(12) <= 178435136773443810
  7. # a(13) <= 4961806417984478790
  8. # a(14) <= 1686255045377006404458210
  9. # a(15) <= 93298412716133272158996030
  10. # a(16) <= 1390082695873048405486577989005
  11. # a(17) <= 107958287712130168652012007653865
  12. # a(18) <= 12494948876051158258450723218329034
  13. # a(19) <= 1110590345888910601135536079323125130
  14. # a(20) <= 81073095249890473882894133790588134490
  15. use 5.020;
  16. use warnings;
  17. use experimental qw(signatures);
  18. use Math::GMPz;
  19. use ntheory qw(:all);
  20. sub squarefree_almost_primes ($n, $k, $factors, $callback) {
  21. my $factors_end = $#{$factors};
  22. if ($k == 0) {
  23. $callback->(1);
  24. return;
  25. }
  26. if ($k == 1) {
  27. $callback->($_) for @$factors;
  28. return;
  29. }
  30. sub ($m, $k, $i = 0) {
  31. if ($k == 1) {
  32. my $L = divint($n, $m);
  33. foreach my $j ($i .. $factors_end) {
  34. my $q = $factors->[$j];
  35. last if ($q > $L);
  36. $callback->(mulint($m, $q));
  37. }
  38. return;
  39. }
  40. my $L = rootint(divint($n, $m), $k);
  41. foreach my $j ($i .. $factors_end - 1) {
  42. my $q = $factors->[$j];
  43. last if ($q > $L);
  44. __SUB__->(mulint($m, $q), $k - 1, $j + 1);
  45. }
  46. }->(1, $k);
  47. return;
  48. }
  49. sub isok ($n) {
  50. is_power(euler_phi($n));
  51. }
  52. my @smooth_primes;
  53. foreach my $p (@{primes(2e8)}) {
  54. if ($p == 2) {
  55. push @smooth_primes, $p;
  56. next;
  57. }
  58. if (is_smooth($p-1, 3)) {
  59. push @smooth_primes, $p;
  60. }
  61. }
  62. my $multiple = Math::GMPz->new(1);
  63. @smooth_primes = grep { gcd($multiple, $_) == 1 } @smooth_primes;
  64. my %table;
  65. my $v = 17;
  66. my $limit = Math::GMPz->new("936857601305665809803096700000000");
  67. $limit /= $multiple;
  68. squarefree_almost_primes($limit, $v - prime_omega($multiple), \@smooth_primes, sub ($k) {
  69. my $n = $multiple * $k;
  70. my $p = isok($n);
  71. if ($p == $v) {
  72. say "a($p) = $n -> ", join(' * ', map { "$_->[0]^$_->[1]" } factor_exp($n));
  73. push @{$table{$p}}, $n;
  74. }
  75. });
  76. say '';
  77. foreach my $k (sort { $a <=> $b } keys %table) {
  78. say "a($k) <= ", vecmin(@{$table{$k}});
  79. }