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 list = [] |
|||
var digits = @(^base) |
|||
var even_digits = digits.grep { .is_even } |
|||
var odd_digits = digits.grep { .is_odd } |
|||
for k in (0 .. upto.len(base)-1) { |
|||
even_digits.variations_with_repetition(k, {|*a| |
|||
next if (a.last == 0) |
|||
break if ([1, a...].digits2num(base) > upto) |
|||
odd_digits.each {|d| |
|||
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) |
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) |
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> |