palindromic_generation.pl 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/perl
  2. # Palindromic pseudoprimes (base 2).
  3. # https://oeis.org/A068445
  4. # Known terms:
  5. # 101101, 129921, 1837381, 127665878878566721, 1037998220228997301
  6. use 5.020;
  7. use strict;
  8. use warnings;
  9. use ntheory qw(is_pseudoprime is_prime);
  10. use experimental qw(signatures);
  11. my $count = 0;
  12. # Observation: the next term cannot be even, therefore it must end with an odd digit.
  13. #my $n = "31169999999999996113"; # the next term is greater than this
  14. my $n = "114149999979999941411";
  15. my @d = split(//, $n);
  16. while (1) {
  17. my $l = $#d;
  18. my $i = (($l + 2) >> 1) - 1;
  19. while ($i >= 0 and $d[$i] == 9) {
  20. $d[$i] = 0;
  21. $d[$l - $i] = 0;
  22. $i--;
  23. }
  24. if ($i >= 0) {
  25. $d[$i]++;
  26. $d[$l - $i] = $d[$i];
  27. }
  28. else {
  29. @d = (0) x (scalar(@d) + 1);
  30. $d[0] = 1;
  31. $d[-1] = 1;
  32. }
  33. my $t = join('', @d);
  34. if (++$count % 1_000_000 == 0) {
  35. $count = 0;
  36. say "Testing: $t";
  37. }
  38. if (is_pseudoprime($t, 2) and !is_prime($t)) {
  39. die "Found new term: $t";
  40. }
  41. }