prog.pl 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/perl
  2. # Numbers that are the sum of m = 5 successive primes and also the product of m = 5 (other) successive primes.
  3. # https://oeis.org/A323052
  4. use 5.014;
  5. use strict;
  6. use warnings;
  7. use IO::File;
  8. use Math::GMPz;
  9. use Math::Prime::Util::GMP qw(vecprod vecsum);
  10. use ntheory qw(forprimes next_prime prev_prime is_prime);
  11. open my $fh, '>', 'b.txt';
  12. $fh->autoflush(1);
  13. sub is_sum {
  14. my ($n) = @_;
  15. my $k = prev_prime(($n / 5) + 1);
  16. while (1) {
  17. my $p = $k;
  18. my @terms = $p;
  19. foreach my $i (1 .. 4) {
  20. $p = next_prime($p);
  21. push @terms, $p;
  22. }
  23. my $r = Math::GMPz->new(vecsum(@terms));
  24. if ($r < $n) {
  25. return 0;
  26. }
  27. if ($r == $n) {
  28. return 1;
  29. }
  30. $k = prev_prime($k);
  31. }
  32. }
  33. my $k = 1;
  34. forprimes {
  35. my $p = $_;
  36. my @factor = ($p);
  37. foreach my $i (1 .. 4) {
  38. $p = next_prime($p);
  39. push @factor, $p;
  40. }
  41. my $n = Math::GMPz->new(vecprod(@factor));
  42. if (is_sum($n)) {
  43. say $fh "$k $n";
  44. say "$k $n";
  45. ++$k;
  46. }
  47. if ($k > 10_000) {
  48. exit;
  49. }
  50. } 1e9;