left-right_truncatable_primes.sf 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/ruby
  2. # Generate both-truncatable primes of the form "abXcd", such that the following terms are all prime numbers:
  3. # abXcd
  4. # abXc
  5. # bXcd
  6. # bXc
  7. # bX
  8. # Xc
  9. # Definition:
  10. # 1. Removing a digit from either side, the number remains prime.
  11. # 2. Removing both digits, the number is still prime.
  12. # 3. Continuing this process, we end up with X.
  13. # Full sequence for prime numbers X below 10:
  14. # 2, 3, 5, 7, 131, 137, 173, 179, 373, 379, 431, 479, 673, 971, 21319, 33739, 54799, 63793, 66733, 76733, 91373, 91733, 94793, 2913739, 3667333, 9637937, 696379373, 896379373
  15. # See also:
  16. # https://www.youtube.com/watch?v=azL5ehbw_24
  17. # https://en.wikipedia.org/wiki/Truncatable_prime
  18. func both_truncatable_primes(p) {
  19. var seq = [p]
  20. for n in (1..9) {
  21. # New left-truncatable prime
  22. Number("#{n}#{p}").is_prime || next
  23. for m in ([1, 3, 7, 9]) {
  24. # New right-truncatable prime
  25. Number("#{p}#{m}").is_prime || next
  26. # New left and right truncatable prime
  27. var t = Number("#{n}#{p}#{m}")
  28. if (t.is_prime) {
  29. seq << both_truncatable_primes(t)...
  30. }
  31. }
  32. }
  33. return seq
  34. }
  35. var seq = []
  36. 10.primes.each {|p|
  37. seq << both_truncatable_primes(p)...
  38. }
  39. say seq.sort