-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleft-right_truncatable_primes.sf
53 lines (39 loc) · 1.27 KB
/
left-right_truncatable_primes.sf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/ruby
# Generate both-truncatable primes of the form "abXcd", such that the following terms are all prime numbers:
# abXcd
# abXc
# bXcd
# bXc
# bX
# Xc
# Definition:
# 1. Removing a digit from either side, the number remains prime.
# 2. Removing both digits, the number is still prime.
# 3. Continuing this process, we end up with X.
# Full sequence for prime numbers X below 10:
# 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
# See also:
# https://www.youtube.com/watch?v=azL5ehbw_24
# https://en.wikipedia.org/wiki/Truncatable_prime
func both_truncatable_primes(p) {
var seq = [p]
for n in (1..9) {
# New left-truncatable prime
Number("#{n}#{p}").is_prime || next
for m in ([1, 3, 7, 9]) {
# New right-truncatable prime
Number("#{p}#{m}").is_prime || next
# New left and right truncatable prime
var t = Number("#{n}#{p}#{m}")
if (t.is_prime) {
seq << both_truncatable_primes(t)...
}
}
}
return seq
}
var seq = []
10.primes.each {|p|
seq << both_truncatable_primes(p)...
}
say seq.sort