-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathright_truncatable_primes.sf
More file actions
47 lines (38 loc) · 953 Bytes
/
right_truncatable_primes.sf
File metadata and controls
47 lines (38 loc) · 953 Bytes
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
#!/usr/bin/ruby
# Generate right-truncatable primes that begin with a given prefix (without truncating the prefix).
# See also:
# https://www.youtube.com/watch?v=azL5ehbw_24
# https://en.wikipedia.org/wiki/Truncatable_prime
func right_truncatable_primes(p) {
var seq = [p]
for n in ([1, 3, 7, 9]) {
var t = Number("#{p}#{n}")
if (t.is_prime) {
seq << right_truncatable_primes(t)...
}
}
return seq
}
#
## Find the largest right-truncatable prime that begins with "n" and remains prime
## after each digit removed from the right, up to (but not including) the prefix "n".
#
for n in (1..15) {
say ("a(#{n}) = ", right_truncatable_primes(n).max)
}
__END__
a(1) = 1979339339
a(2) = 29399999
a(3) = 37337999
a(4) = 4391339
a(5) = 59393339
a(6) = 6733997
a(7) = 73939133
a(8) = 839
a(9) = 9719
a(10) = 103997939939
a(11) = 113
a(12) = 12791333
a(13) = 13999133
a(14) = 149399
a(15) = 15797