Emirp primes
1
func forprimes(a, b, callback) {
2
for (var p = a.dec.next_prime; p <= b; p.next_prime!) {
3
callback(p)
4
}
5
}
6
7
func is_emirp(p) {
8
var str = Str(p)
9
var rev = str.reverse
10
(str != rev) && is_prime(Num(rev))
11
}
12
13
func emirp_list(count) {
14
var i = 13
15
var inc = (100 + 10*count)
16
var n = []
17
while (n.len < count) {
18
forprimes(i, i+inc - 1, {|p|
19
is_emirp(p) && (n << p)
20
})
21
(i, inc) = (i+inc, int(inc * 1.03) + 1000)
22
}
23
n.splice(count)
24
return n
25
}
26
27
say ("First 20: ", emirp_list(20).join(' '))
28
say ("Between 7700 and 8000: ", gather {
29
forprimes(7700, 8000, {|p| is_emirp(p) && take(p) })
30
}.join(' '))
31
say ("The 10,000'th emirp: ", emirp_list(10000)[-1])
Copied!

Output:

1
First 20: 13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389
2
Between 7700 and 8000: 7717 7757 7817 7841 7867 7879 7901 7927 7949 7951 7963
3
The 10,000'th emirp: 948349
Copied!
Last modified 1yr ago
Copy link
Contents
Output: