prog.pl 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/perl
  2. # a(n) is the least number k such that the average number of prime divisors of {1..k} counted with multiplicity is >= n.
  3. # https://oeis.org/A336304
  4. # Known terms:
  5. # 1, 4, 32, 2178, 416417176
  6. use strict;
  7. use warnings;
  8. use 5.020;
  9. use Math::AnyNum qw(bsearch_le);
  10. use ntheory qw(:all);
  11. use experimental qw(signatures);
  12. sub sum_of_exponents_of_factorial_2 ($n) {
  13. my $s = sqrtint($n);
  14. my $total = 0;
  15. for my $k (1 .. $s) {
  16. $total += prime_power_count(divint($n,$k));
  17. $total += divint($n,$k) if is_prime_power($k);
  18. }
  19. $total -= prime_power_count($s) * $s;
  20. return $total;
  21. }
  22. sub a {
  23. my ($n) = @_;
  24. #return sum_of_exponents_of_factorial_2($n);
  25. return 0 if ($n <= 1);
  26. my $s = sqrtint($n);
  27. my $u = int($n/($s + 1));
  28. my $total = 0;
  29. my $prev = prime_power_count($n);
  30. for my $k (1 .. $s) {
  31. my $curr = prime_power_count(int($n/($k+1)));
  32. $total += $k * ($prev - $curr);
  33. $prev = $curr;
  34. }
  35. forprimes {
  36. foreach my $k (1 .. logint($u, $_)) {
  37. $total += int($n / $_**$k);
  38. }
  39. } $u;
  40. return $total;
  41. }
  42. my $n = 5;
  43. my $x = 2;
  44. my $y = 2*$x;
  45. while (a($y) / $y < $n) {
  46. say "Checking range: ($x, $y)";
  47. ($x, $y) = ($y, 2*$y);
  48. }
  49. say "Sieving: ($x, $y)";
  50. my $v = bsearch_le($x, $y, sub {
  51. my ($k) = @_;
  52. say "Checking: $k";
  53. a($k) / $k <=> $n;
  54. });
  55. say a($v)/$v;
  56. #CORE::say a(416417176);
  57. #printf("%s\n", join(', ', map { a($_) } 0..100));