-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleft_truncatable_primes_in_base.sf
49 lines (38 loc) · 1.82 KB
/
left_truncatable_primes_in_base.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
#!/usr/bin/ruby
# Generate left-truncatable primes in a given base.
# See also:
# https://oeis.org/A103443 -- Largest left-truncatable prime in base n (given in decimal).
# https://oeis.org/A076623 -- Total number of left truncatable primes (without zeros) in base n.
# https://www.youtube.com/watch?v=azL5ehbw_24
# https://en.wikipedia.org/wiki/Truncatable_prime
# https://rosettacode.org/wiki/Find_largest_left_truncatable_prime_in_a_given_base
func generate_from_suffix(p, base) {
var seq = [p]
for n in (1 ..^ base) {
var t = [p..., n]
if (is_prime(digits2num(t, base))) {
seq << __FUNC__(t, base)...
}
}
return seq
}
func left_truncatable_primes(base) { # finite sequence for each base
var prime_digits = (base-1 -> primes) # prime digits < base
prime_digits.map {|p| generate_from_suffix([p], base)... }\
.map {|t| digits2num(t, base) }\
.sort
}
for n in (3..9) {
var ltp = left_truncatable_primes(n)
say ("There are #{'%4d' % ltp.len} left-truncatable primes in base #{'%2d' % n}, where largest is #{ltp.max}")
}
__END__
There are 3 left-truncatable primes in base 3, where largest is 23
There are 16 left-truncatable primes in base 4, where largest is 4091
There are 15 left-truncatable primes in base 5, where largest is 7817
There are 454 left-truncatable primes in base 6, where largest is 4836525320399
There are 22 left-truncatable primes in base 7, where largest is 817337
There are 446 left-truncatable primes in base 8, where largest is 14005650767869
There are 108 left-truncatable primes in base 9, where largest is 1676456897
There are 4260 left-truncatable primes in base 10, where largest is 357686312646216567629137
There are 75 left-truncatable primes in base 11, where largest is 2276005673