prog.pl 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/perl
  2. # Numbers k such that the number of primes <= k is equal to the sum of primes from the smallest prime factor of k to the largest prime factor of k.
  3. # https://oeis.org/A074210
  4. use 5.014;
  5. use ntheory qw(:all);
  6. use experimental qw(signatures);
  7. sub isok {
  8. my ($n) = @_;
  9. my @f = factor($n);
  10. prime_count($n) == sum_primes($f[0], $f[-1]);
  11. }
  12. foreach my $n (4, 12, 30, 272, 4717, 5402, 18487, 20115, 28372, 33998, 111040, 115170, 456975, 821586, 1874660, 4029676, 4060029, 59497900, 232668002, 313128068, 529436220) {
  13. if (isok($n)) {
  14. say "$n -- ok";
  15. }
  16. else {
  17. say "$n -- not ok";
  18. }
  19. }
  20. my $from = prev_prime(529436220) - 1;
  21. my $pi = prime_count($from);
  22. sub arithmetic_sum_discrete ($x, $y, $z) {
  23. (int(($y - $x) / $z) + 1) * ($z * int(($y - $x) / $z) + 2 * $x) / 2;
  24. }
  25. sub prime_sum_approx ($n) {
  26. $n * $n / 2 / log($n);
  27. }
  28. foreach my $n ($from .. $from + 1e5) {
  29. if (is_prime($n)) {
  30. ++$pi;
  31. next;
  32. }
  33. my @f = factor($n);
  34. if (prime_sum_approx($f[-1]) - prime_sum_approx($f[0]) > $pi) {
  35. next;
  36. }
  37. if (arithmetic_sum_discrete($f[0], $f[-1], 1) < $pi) {
  38. next;
  39. }
  40. if ($pi == sum_primes($f[0], $f[-1])) {
  41. say "Found: $n";
  42. }
  43. }