lucas-pratt_prime_records.pl 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 12 May 2019
  4. # https://github.com/trizen
  5. # Count the number of nodes in the Lucas-Pratt primality tree, rooted at a given prime.
  6. # See also:
  7. # https://oeis.org/A037231 -- Primes which set a new record for length of Pratt certificate.
  8. # https://oeis.org/A130790 -- Number of nodes in the Lucas-Pratt primality tree rooted at prime(n).
  9. use 5.020;
  10. use warnings;
  11. use ntheory qw(:all);
  12. use Memoize qw(memoize);
  13. use experimental qw(signatures);
  14. memoize('lucas_pratt_primality_tree_count');
  15. sub lucas_pratt_primality_tree_count ($p, $r = -1) {
  16. return 0 if ($p <= 1);
  17. return 1 if ($p == 2);
  18. vecsum(map { __SUB__->($_->[0], $r) } factor_exp($p + $r));
  19. }
  20. sub lucas_pratt_prime_records ($r = -1, $upto = 1e6) {
  21. my $max = 0;
  22. my @primes;
  23. forprimes {
  24. my $t = lucas_pratt_primality_tree_count($_, $r);
  25. if ($t > $max) {
  26. $max = $t;
  27. push @primes, $_;
  28. }
  29. } $upto;
  30. return @primes;
  31. }
  32. say "p-1: ", join(', ', lucas_pratt_prime_records(-1, 1e6)); # A037231
  33. say "p+1: ", join(', ', lucas_pratt_prime_records(+1, 1e6));
  34. __END__
  35. p-1: 2, 7, 23, 43, 139, 283, 659, 1319, 5179, 9227, 23159, 55399, 148439, 366683, 793439, 1953839, 4875119, 9750239
  36. p+1: 2, 5, 19, 29, 73, 173, 569, 1109, 2917, 5189, 10729, 21169, 42337, 84673, 254021, 508037, 1287457, 3787969, 7575937