-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis_both_truncatable_prime.sf
64 lines (46 loc) · 2.06 KB
/
is_both_truncatable_prime.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
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/ruby
# Daniel "Trizen" Șuteu
# Date: 16 January 2018
# https://github.com/trizen
# Check if a given number is left-truncatable and right-truncatable in a given base.
# Maximum value for each base is given in the following OEIS sequence:
# https://oeis.org/A323137
# See also:
# https://www.youtube.com/watch?v=azL5ehbw_24
# https://en.wikipedia.org/wiki/Truncatable_prime
# Left-truncatable primes in base 10:
# https://oeis.org/A024785
# Right-truncatable primes in base 10:
# https://oeis.org/A024770
# Other related sequences:
# https://oeis.org/A076586 - Total number of right truncatable primes in base n.
# https://oeis.org/A076623 - Total number of left truncatable primes (without zeros) in base n.
# https://oeis.org/A323390 - Total number of primes that are both left-truncatable and right-truncatable in base n.
# 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.
func is_left_truncatable_prime(n, base) {
var digits = n.digits(base)
while (digits) {
digits[-1] || return false
is_prime(digits2num(digits, base)) || return false
digits.first!(-1)
}
return true
}
func is_right_truncatable_prime(n, base) {
var digits = n.digits(base)
while (digits) {
is_prime(digits2num(digits, base)) || return false
digits.last!(-1)
}
return true
}
func is_both_truncatable(n, base) {
is_left_truncatable_prime(n, base) &&
is_right_truncatable_prime(n, base)
}
say is_both_truncatable(21335388527, 30) #=> true
say is_both_truncatable(1591175082967, 64) #=> true
say is_both_truncatable(1272571820725769, 42) #=> true
say is_both_truncatable(200416308070405393, 60) #=> true
say { is_left_truncatable_prime(_, 10) }.first(20) #=> [2, 3, 5, 7, 13, 17, 23, 37, 43, 47, 53, 67, 73, 83, 97, 113, 137, 167, 173, 197]
say { is_right_truncatable_prime(_, 10) }.first(20) #=> [2, 3, 5, 7, 23, 29, 31, 37, 53, 59, 71, 73, 79, 233, 239, 293, 311, 313, 317, 373]