-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_primes_with_only_one_odd_digit.sf
65 lines (50 loc) · 1.74 KB
/
generate_primes_with_only_one_odd_digit.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
65
#!/usr/bin/ruby
# Daniel "Trizen" Șuteu
# Date: 28 July 2021
# https://github.com/trizen
# Generate primes that have only one odd digit (at the end).
# See also:
# https://oeis.org/A068690
# https://rosettacode.org/wiki/Primes_which_contain_only_one_odd_number
func primes_with_one_odd_digit(upto, base = 10) {
upto = prev_prime(upto+1)
var list = []
var digits = @(^base)
var even_digits = digits.grep { .is_even }
var odd_digits = digits.grep { .is_odd && .is_coprime(base) }
list << digits.grep { .is_odd && .is_prime && !.is_coprime(base) }...
for k in (0 .. upto.ilog(base)) {
even_digits.variations_with_repetition(k, {|*a|
next if (a.last == 0)
var v = a.digits2num(base)
odd_digits.each {|d|
var n = (v*base + d)
list << n if (n.is_prime && (n <= upto))
}
})
}
list.sort
}
with (1000) {|n|
var list = primes_with_one_odd_digit(n)
say "There are #{list.len} primes under #{n.commify} which contain only one odd digit:"
list.each_slice(9, {|*a| say a.map { '%3s' % _ }.join(' ') })
}
say ''
for k in (1..6) {
var count = primes_with_one_odd_digit(10**k).len
say "There are #{'%6s' % count.commify} such primes <= 10^#{k}"
}
__END__
There are 45 primes under 1,000 which contain only one odd digit:
3 5 7 23 29 41 43 47 61
67 83 89 223 227 229 241 263 269
281 283 401 409 421 443 449 461 463
467 487 601 607 641 643 647 661 683
809 821 823 827 829 863 881 883 887
There are 3 such primes <= 10^1
There are 12 such primes <= 10^2
There are 45 such primes <= 10^3
There are 171 such primes <= 10^4
There are 619 such primes <= 10^5
There are 2,560 such primes <= 10^6