Primes which contain only one odd digit: Difference between revisions

Content added Content deleted
(Added Sidef)
m (→‎{{header|Sidef}}: minor optimizations and simplifications)
Line 367: Line 367:
=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func primes_with_one_odd_digit(upto, base = 10) {
<lang ruby>func primes_with_one_odd_digit(upto, base = 10) {
Enumerator({|callback|


var digits = @(^base)
var list = []
var digits = @(^base)


var even_digits = digits.grep { .is_even }
var even_digits = digits.grep { .is_even }
var odd_digits = digits.grep { .is_odd }
var odd_digits = digits.grep { .is_odd }


for k in (0 .. upto.len(base)-1) {
for k in (0 .. upto.len(base)-1) {
even_digits.variations_with_repetition(k, {|*a|
even_digits.variations_with_repetition(k, {|*a|
break if ([1, a...].digits2num(base) > upto)
next if (a.last == 0)
odd_digits.each {|d|
break if ([1, a...].digits2num(base) > upto)
var n = [d, a...].digits2num(base)
odd_digits.each {|d|
callback(n) if ((n <= upto) && n.is_prime)
var n = [d, a...].digits2num(base)
}
list << n if ((n <= upto) && n.is_prime)
})
}
}
})
})
}

list.sort
}
}


with (1e3) {|n|
with (1e3) {|n|
var list = primes_with_one_odd_digit(1000).to_a.uniq.sort
var list = primes_with_one_odd_digit(1000)
say "There are #{list.len} primes under #{n.commify} which contain only one odd digit:"
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(' ') })
list.each_slice(9, {|*a| say a.map { '%3s' % _ }.join(' ') })
Line 395: Line 397:


for k in (1..8) {
for k in (1..8) {
var count = primes_with_one_odd_digit(10**k).to_a.uniq.len
var count = primes_with_one_odd_digit(10**k).len
say "There are #{'%6s' % count.commify} such primes <= 10^#{k}"
say "There are #{'%6s' % count.commify} such primes <= 10^#{k}"
}</lang>
}</lang>