both_truncatable_primes_in_base.sf 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # Date: 13 January 2018
  4. # https://github.com/trizen
  5. # Both-trucatable primes in a given base.
  6. # Optimization:
  7. # there are fewer right-truncatable primes than are left-truncatable primes, therefore we generate
  8. # only the right-truncatable primes and check which ones of those are also left-truncatable primes.
  9. # Maximum value for each base is given in the following OEIS sequence:
  10. # https://oeis.org/A323137
  11. # See also:
  12. # https://www.youtube.com/watch?v=azL5ehbw_24
  13. # https://en.wikipedia.org/wiki/Truncatable_prime
  14. # Related sequences:
  15. # https://oeis.org/A076586 - Total number of right truncatable primes in base n.
  16. # https://oeis.org/A076623 - Total number of left truncatable primes (without zeros) in base n.
  17. # https://oeis.org/A323390 - Total number of primes that are both left-truncatable and right-truncatable in base n.
  18. # 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.
  19. func is_left_truncatable_prime(n, base) {
  20. for (var r = base; r < n; r *= base) {
  21. n - r*idiv(n, r) -> is_prime || return false
  22. }
  23. return true
  24. }
  25. func generate_from_prefix(p, base, digits) {
  26. var seq = [p]
  27. digits.each {|d|
  28. var n = (p*base + d)
  29. if (n.is_prime) {
  30. seq << __FUNC__(n, base, digits).grep { is_left_truncatable_prime(_, base) }...
  31. }
  32. }
  33. return seq
  34. }
  35. func both_truncatable_primes(base) { # finite sequence for each base
  36. var prime_digits = (base-1 -> primes) # prime digits < base
  37. var digits = @(1 ..^ base)
  38. prime_digits.map {|p| generate_from_prefix(p, base, digits)... }\
  39. .sort
  40. }
  41. for base in (3..15) {
  42. var a = both_truncatable_primes(base)
  43. say "There are #{'%3d' % a.len} both-truncatable primes in base #{'%2d' % base} where largest is #{a[-1]}"
  44. }
  45. __END__
  46. There are 2 both-truncatable primes in base 3 where largest is 23
  47. There are 3 both-truncatable primes in base 4 where largest is 11
  48. There are 5 both-truncatable primes in base 5 where largest is 67
  49. There are 9 both-truncatable primes in base 6 where largest is 839
  50. There are 7 both-truncatable primes in base 7 where largest is 37
  51. There are 22 both-truncatable primes in base 8 where largest is 1867
  52. There are 8 both-truncatable primes in base 9 where largest is 173
  53. There are 15 both-truncatable primes in base 10 where largest is 739397
  54. There are 6 both-truncatable primes in base 11 where largest is 79
  55. There are 35 both-truncatable primes in base 12 where largest is 105691
  56. There are 11 both-truncatable primes in base 13 where largest is 379
  57. There are 37 both-truncatable primes in base 14 where largest is 37573
  58. There are 17 both-truncatable primes in base 15 where largest is 647
  59. There are 22 both-truncatable primes in base 16 where largest is 3389
  60. There are 12 both-truncatable primes in base 17 where largest is 631
  61. There are 69 both-truncatable primes in base 18 where largest is 202715129
  62. There are 12 both-truncatable primes in base 19 where largest is 211
  63. There are 68 both-truncatable primes in base 20 where largest is 155863
  64. There are 18 both-truncatable primes in base 21 where largest is 1283
  65. There are 44 both-truncatable primes in base 22 where largest is 787817
  66. There are 13 both-truncatable primes in base 23 where largest is 439
  67. There are 145 both-truncatable primes in base 24 where largest is 109893629
  68. There are 16 both-truncatable primes in base 25 where largest is 577
  69. There are 47 both-truncatable primes in base 26 where largest is 4195880189
  70. There are 20 both-truncatable primes in base 27 where largest is 1811
  71. There are 77 both-truncatable primes in base 28 where largest is 14474071
  72. There are 13 both-truncatable primes in base 29 where largest is 379