Primes which contain only one odd digit: Difference between revisions
Content added Content deleted
m (Peak moved page Primes which contain only one odd number to Primes which contain only one odd digit: clarity) |
|||
Line 353: | Line 353: | ||
Count of matches below 10E6: |
Count of matches below 10E6: |
||
2560</pre> |
2560</pre> |
||
=={{header|jq}}== |
|||
{{works with|jq}} |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
As noted in the [[#Julia|Julia entry]], if only one digit of a prime is odd, then that digit is in the ones place. The first solution presented here uses this observation to generate plausible candidates. The second solution is more brutish and slower but simpler. |
|||
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`. |
|||
===Fast solution=== |
|||
<lang jq>### Preliminaries |
|||
def count(s): reduce s as $x (null; .+1); |
|||
def emit_until(cond; stream): |
|||
label $out | stream | if cond then break $out else . end; |
|||
# Output: an unbounded stream |
|||
def primes_with_exactly_one_odd_digit: |
|||
# Output: a stream of candidate strings, in ascending numerical order |
|||
def candidates: |
|||
# input is $width |
|||
def evens: |
|||
. as $width |
|||
| if $width == 0 then "" |
|||
else ("0","2","4","6","8") as $i |
|||
| ((.-1)|evens) as $j |
|||
| "\($i)\($j)" |
|||
end; |
|||
("2","4","6","8") as $leading |
|||
| evens as $even |
|||
| ("1","3","5","7","9") as $odd |
|||
| "\($leading)\($even)\($odd)"; |
|||
(3,5,7), |
|||
(range(0; infinite) |
|||
| candidates | tonumber |
|||
| select(is_prime)) ; |
|||
### The Task |
|||
emit_until(. > 1000; primes_with_exactly_one_odd_digit), |
|||
"\nThe number of primes less than 1000000 with exactly one odd digits is \(count(emit_until(. > 1000000; primes_with_exactly_one_odd_digit)))."/lang> |
|||
{{out}} |
|||
<pre> |
|||
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 |
|||
The number of primes less than 1000000 with exactly one odd digits is 2560. |
|||
</pre> |
|||
=== A simpler but slower solution === |
|||
<lang jq># Input is assumed to be prime. |
|||
# So we only need check the other digits are all even. |
|||
def prime_has_exactly_one_odd_digit: |
|||
if . == 2 then false |
|||
# The codepoints for the odd digits are also odd: [49,51,53,55,57] |
|||
else all(tostring | explode[:-1][]; . % 2 == 0) |
|||
end; |
|||
def primes_with_exactly_one_odd_digit_crudely: |
|||
# It is much faster to check for primality afterwards. |
|||
range(3; infinite; 2) |
|||
| select(prime_has_exactly_one_odd_digit and is_prime);</lang> |
|||
</lang> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |