both_truncatable_primes_in_base.pl 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 12 January 2019
  4. # Edit: 28 March 2023
  5. # https://github.com/trizen
  6. # Generate the entire sequence of both-truncatable primes in a given base.
  7. # Optimization:
  8. # there are far fewer right-truncatable primes than are left-truncatable primes,
  9. # so we can generate only the RTPs and then check which ones are also LTPs.
  10. # Maximum value for each base is given in the following OEIS sequence:
  11. # https://oeis.org/A323137
  12. # Total number of primes that are both left-truncatable and right-truncatable in base n:
  13. # https://oeis.org/A323390
  14. # See also:
  15. # https://www.youtube.com/watch?v=azL5ehbw_24
  16. # https://en.wikipedia.org/wiki/Truncatable_prime
  17. # Related sequences:
  18. # https://oeis.org/A076586 - Total number of right truncatable primes in base n.
  19. # https://oeis.org/A076623 - Total number of left truncatable primes (without zeros) in base n.
  20. # https://oeis.org/A323390 - Total number of primes that are both left-truncatable and right-truncatable in base n.
  21. # https://oeis.org/A323396 - Irregular array read by rows, where T(n, k) is the k-th prime that is both left-truncatable and right-truncatable in base n.
  22. use 5.036;
  23. use ntheory qw(primes vecmax is_prime);
  24. use Math::Prime::Util::GMP qw(divint mulint addint subint);
  25. sub is_left_truncatable ($n, $base) {
  26. for (my $r = $base ; $r < $n ; $r = mulint($r, $base)) {
  27. is_prime(subint($n, mulint($r, divint($n, $r)))) || return 0;
  28. }
  29. return 1;
  30. }
  31. sub generate_from_prefix ($p, $base) {
  32. my @seq = ($p);
  33. foreach my $d (1 .. $base - 1) {
  34. my $n = addint(mulint($p, $base), $d);
  35. if (is_prime($n)) {
  36. push @seq, grep { is_left_truncatable($_, $base) } generate_from_prefix($n, $base);
  37. }
  38. }
  39. return @seq;
  40. }
  41. sub both_truncatable_primes_in_base ($base) {
  42. return if $base <= 2;
  43. my @truncatable;
  44. foreach my $p (@{primes(2, $base - 1)}) {
  45. push @truncatable, generate_from_prefix($p, $base);
  46. }
  47. return @truncatable;
  48. }
  49. foreach my $base (3 .. 36) {
  50. my @t = both_truncatable_primes_in_base($base);
  51. printf("There are %3d both-truncatable primes in base %2d where largest is %s\n", scalar(@t), $base, vecmax(@t));
  52. }
  53. __END__
  54. There are 2 both-truncatable primes in base 3 where largest is 23
  55. There are 3 both-truncatable primes in base 4 where largest is 11
  56. There are 5 both-truncatable primes in base 5 where largest is 67
  57. There are 9 both-truncatable primes in base 6 where largest is 839
  58. There are 7 both-truncatable primes in base 7 where largest is 37
  59. There are 22 both-truncatable primes in base 8 where largest is 1867
  60. There are 8 both-truncatable primes in base 9 where largest is 173
  61. There are 15 both-truncatable primes in base 10 where largest is 739397
  62. There are 6 both-truncatable primes in base 11 where largest is 79
  63. There are 35 both-truncatable primes in base 12 where largest is 105691
  64. There are 11 both-truncatable primes in base 13 where largest is 379
  65. There are 37 both-truncatable primes in base 14 where largest is 37573
  66. There are 17 both-truncatable primes in base 15 where largest is 647
  67. There are 22 both-truncatable primes in base 16 where largest is 3389
  68. There are 12 both-truncatable primes in base 17 where largest is 631
  69. There are 69 both-truncatable primes in base 18 where largest is 202715129
  70. There are 12 both-truncatable primes in base 19 where largest is 211
  71. There are 68 both-truncatable primes in base 20 where largest is 155863
  72. There are 18 both-truncatable primes in base 21 where largest is 1283
  73. There are 44 both-truncatable primes in base 22 where largest is 787817
  74. There are 13 both-truncatable primes in base 23 where largest is 439
  75. There are 145 both-truncatable primes in base 24 where largest is 109893629
  76. There are 16 both-truncatable primes in base 25 where largest is 577
  77. There are 47 both-truncatable primes in base 26 where largest is 4195880189
  78. There are 20 both-truncatable primes in base 27 where largest is 1811
  79. There are 77 both-truncatable primes in base 28 where largest is 14474071
  80. There are 13 both-truncatable primes in base 29 where largest is 379
  81. There are 291 both-truncatable primes in base 30 where largest is 21335388527
  82. There are 15 both-truncatable primes in base 31 where largest is 2203
  83. There are 89 both-truncatable primes in base 32 where largest is 1043557
  84. There are 27 both-truncatable primes in base 33 where largest is 2939
  85. There are 74 both-truncatable primes in base 34 where largest is 42741029
  86. There are 20 both-truncatable primes in base 35 where largest is 2767
  87. There are 241 both-truncatable primes in base 36 where largest is 50764713107
  88. There are 18 both-truncatable primes in base 37 where largest is 853
  89. There are 106 both-truncatable primes in base 38 where largest is 65467229
  90. There are 25 both-truncatable primes in base 39 where largest is 4409
  91. There are 134 both-truncatable primes in base 40 where largest is 8524002457
  92. There are 15 both-truncatable primes in base 41 where largest is 113
  93. There are 450 both-truncatable primes in base 42 where largest is 1272571820725769
  94. There are 23 both-truncatable primes in base 43 where largest is 4861
  95. There are 144 both-truncatable primes in base 44 where largest is 3215447359
  96. There are 33 both-truncatable primes in base 45 where largest is 5897
  97. There are 131 both-truncatable primes in base 46 where largest is 8542971469
  98. There are 24 both-truncatable primes in base 47 where largest is 1741
  99. There are 491 both-truncatable primes in base 48 where largest is 531866995189
  100. There are 27 both-truncatable primes in base 49 where largest is 6421
  101. There are 235 both-truncatable primes in base 50 where largest is 297897697
  102. There are 29 both-truncatable primes in base 51 where largest is 2399
  103. There are 187 both-truncatable primes in base 52 where largest is 2276097403
  104. There are 23 both-truncatable primes in base 53 where largest is 2281
  105. There are 575 both-truncatable primes in base 54 where largest is 586812834217
  106. There are 30 both-truncatable primes in base 55 where largest is 7537
  107. There are 218 both-truncatable primes in base 56 where largest is 3086112347
  108. There are 31 both-truncatable primes in base 57 where largest is 9521
  109. There are 183 both-truncatable primes in base 58 where largest is 24666304823
  110. There are 25 both-truncatable primes in base 59 where largest is 9619
  111. There are 1377 both-truncatable primes in base 60 where largest is 200416308070405393
  112. There are 26 both-truncatable primes in base 61 where largest is 2503
  113. There are 247 both-truncatable primes in base 62 where largest is 2467459748009
  114. There are 37 both-truncatable primes in base 63 where largest is 10271
  115. There are 231 both-truncatable primes in base 64 where largest is 1591175082967