Extra primes
Simple solution:
1
say 1e4.primes.grep { .digits.all { .is_prime } && .sumdigits.is_prime }
Copied!

Output:

1
[2, 3, 5, 7, 23, 223, 227, 337, 353, 373, 557, 577, 733, 757, 773, 2333, 2357, 2377, 2557, 2753, 2777, 3253, 3257, 3323, 3527, 3727, 5233, 5237, 5273, 5323, 5527, 7237, 7253, 7523, 7723, 7727]
Copied!
Generate such primes from digits (faster):
1
func extra_primes(upto, base = 10) {
2
3
upto = prev_prime(upto+1)
4
5
var list = []
6
var digits = @(^base)
7
8
var prime_digits = digits.grep { .is_prime }
9
var end_digits = prime_digits.grep { .is_coprime(base) }
10
11
list << prime_digits.grep { !.is_coprime(base) }...
12
13
for k in (0 .. upto.ilog(base)) {
14
prime_digits.variations_with_repetition(k, {|*a|
15
next if ([end_digits[0], a...].digits2num(base) > upto)
16
end_digits.each {|d|
17
var n = [d, a...].digits2num(base)
18
list << n if (n.is_prime && n.sumdigits(base).is_prime)
19
}
20
})
21
}
22
23
list.sort
24
}
25
26
with (1e4) { |n|
27
say "Extra primes <= #{n.commify}:"
28
say extra_primes(n).join(' ')
29
}
30
31
with (1000000000) {|n|
32
say "\nLast 10 extra primes <= #{n.commify}:"
33
say extra_primes(n).last(10).join(' ')
34
}
Copied!

Output:

1
Extra primes <= 10,000:
2
2 3 5 7 23 223 227 337 353 373 557 577 733 757 773 2333 2357 2377 2557 2753 2777 3253 3257 3323 3527 3727 5233 5237 5273 5323 5527 7237 7253 7523 7723 7727
3
4
Last 10 extra primes <= 1,000,000,000:
5
777753773 777755753 777773333 777773753 777775373 777775553 777775577 777777227 777777577 777777773
Copied!
Last modified 3mo ago
Copy link