both_truncatable_primes_in_base.pl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 12 January 2019
  4. # https://github.com/trizen
  5. # Generate the entire sequence of both-truncatable primes in a given base.
  6. # Optimization:
  7. # there are far fewer right-truncatable primes than are left-truncatable primes,
  8. # so we can generate only the RTPs and then check which ones are also LTPs.
  9. # Maximum value for each base is given in the following OEIS sequence:
  10. # https://oeis.org/A323137
  11. # Total number of primes that are both left-truncatable and right-truncatable in base n:
  12. # https://oeis.org/A323390
  13. # See also:
  14. # https://www.youtube.com/watch?v=azL5ehbw_24
  15. # https://en.wikipedia.org/wiki/Truncatable_prime
  16. # Related sequences:
  17. # https://oeis.org/A076586 - Total number of right truncatable primes in base n.
  18. # https://oeis.org/A076623 - Total number of left truncatable primes (without zeros) in base n.
  19. # https://oeis.org/A323390 - Total number of primes that are both left-truncatable and right-truncatable in base n.
  20. # 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.
  21. # New terms:
  22. #~ There are 27 both-truncatable primes in base 49 where largest is 6421
  23. #~ There are 235 both-truncatable primes in base 50 where largest is 297897697
  24. #~ There are 29 both-truncatable primes in base 51 where largest is 2399
  25. #~ There are 187 both-truncatable primes in base 52 where largest is 2276097403
  26. #~ There are 23 both-truncatable primes in base 53 where largest is 2281
  27. #~ There are 575 both-truncatable primes in base 54 where largest is 586812834217
  28. use 5.010;
  29. use strict;
  30. use warnings;
  31. use Math::GMPz;
  32. use ntheory qw(primes vecmax is_prob_prime);
  33. my $t = Math::GMPz::Rmpz_init_set_ui(1);
  34. my $sum = Math::GMPz::Rmpz_init_set_ui(0);
  35. sub digits2num {
  36. my ($arr, $base) = @_;
  37. Math::GMPz::Rmpz_set_ui($t, 1);
  38. Math::GMPz::Rmpz_set_ui($sum, 0);
  39. foreach my $d (@$arr) {
  40. Math::GMPz::Rmpz_addmul_ui($sum, $t, $d);
  41. Math::GMPz::Rmpz_mul_ui($t, $t, $base);
  42. }
  43. Math::GMPz::Rmpz_get_str($sum, 10);
  44. }
  45. sub is_left_truncatable {
  46. my ($n, $base) = @_;
  47. my @copy = @$n;
  48. for (my @arr = shift(@copy); @copy > 0; push(@arr, shift(@copy))) {
  49. is_prob_prime(digits2num(\@arr, $base)) || return 0;
  50. }
  51. return 1;
  52. }
  53. sub right_truncatable_primes {
  54. my ($p, $base) = @_;
  55. my @seq = ($p);
  56. foreach my $n (1 .. $base - 1) {
  57. my @next = ($n, @$p);
  58. if (is_prob_prime(digits2num(\@next, $base))) {
  59. push @seq, grep { is_left_truncatable($_, $base) } right_truncatable_primes(\@next, $base);
  60. }
  61. }
  62. return @seq;
  63. }
  64. sub both_truncatable_primes_in_base {
  65. my ($base) = @_;
  66. if ($base <= 2) {
  67. return;
  68. }
  69. my @right;
  70. foreach my $p (@{primes(2, $base - 1)}) {
  71. push @right, right_truncatable_primes([$p], $base);
  72. }
  73. map { digits2num($_, $base) } @right;
  74. }
  75. foreach my $base (3..30) {
  76. my @t = both_truncatable_primes_in_base($base);
  77. printf("There are %3d both-truncatable primes in base %2d where largest is %s\n", scalar(@t), $base, vecmax(@t));
  78. }
  79. __END__
  80. There are 2 both-truncatable primes in base 3 where largest is 23
  81. There are 3 both-truncatable primes in base 4 where largest is 11
  82. There are 5 both-truncatable primes in base 5 where largest is 67
  83. There are 9 both-truncatable primes in base 6 where largest is 839
  84. There are 7 both-truncatable primes in base 7 where largest is 37
  85. There are 22 both-truncatable primes in base 8 where largest is 1867
  86. There are 8 both-truncatable primes in base 9 where largest is 173
  87. There are 15 both-truncatable primes in base 10 where largest is 739397
  88. There are 6 both-truncatable primes in base 11 where largest is 79
  89. There are 35 both-truncatable primes in base 12 where largest is 105691
  90. There are 11 both-truncatable primes in base 13 where largest is 379
  91. There are 37 both-truncatable primes in base 14 where largest is 37573
  92. There are 17 both-truncatable primes in base 15 where largest is 647
  93. There are 22 both-truncatable primes in base 16 where largest is 3389
  94. There are 12 both-truncatable primes in base 17 where largest is 631
  95. There are 69 both-truncatable primes in base 18 where largest is 202715129
  96. There are 12 both-truncatable primes in base 19 where largest is 211
  97. There are 68 both-truncatable primes in base 20 where largest is 155863
  98. There are 18 both-truncatable primes in base 21 where largest is 1283
  99. There are 44 both-truncatable primes in base 22 where largest is 787817
  100. There are 13 both-truncatable primes in base 23 where largest is 439
  101. There are 145 both-truncatable primes in base 24 where largest is 109893629
  102. There are 16 both-truncatable primes in base 25 where largest is 577
  103. There are 47 both-truncatable primes in base 26 where largest is 4195880189
  104. There are 20 both-truncatable primes in base 27 where largest is 1811
  105. There are 77 both-truncatable primes in base 28 where largest is 14474071
  106. There are 13 both-truncatable primes in base 29 where largest is 379
  107. There are 291 both-truncatable primes in base 30 where largest is 21335388527
  108. There are 15 both-truncatable primes in base 31 where largest is 2203
  109. There are 89 both-truncatable primes in base 32 where largest is 1043557
  110. There are 27 both-truncatable primes in base 33 where largest is 2939
  111. There are 74 both-truncatable primes in base 34 where largest is 42741029