Primes which contain only one odd digit: Difference between revisions

Content added Content deleted
(→‎stretch: ahem, I meant 40*above, <2* without skipping.)
(added AWK)
Line 61: Line 61:


Found 2560 single-odd-digit primes upto 1000000
Found 2560 single-odd-digit primes upto 1000000
</pre>
=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f PRIMES_WHICH_CONTAIN_ONLY_ONE_ODD_NUMBER.AWK
BEGIN {
start = 1
stop = 999
for (i=start; i<=stop; i++) {
if (is_prime(i)) {
if (gsub(/[13579]/,"&",i) == 1) {
rec_odd = sprintf("%s%5d%1s",rec_odd,i,++count_odd%10?"":"\n")
}
if (gsub(/[02468]/,"&",i) == 1) {
rec_even = sprintf("%s%5d%1s",rec_even,i,++count_even%10?"":"\n")
}
}
}
printf("%s\nPrimes which contain only one odd number %d-%d: %d\n\n",rec_odd,start,stop,count_odd)
printf("%s\nPrimes which contain only one even number %d-%d: %d\n\n",rec_even,start,stop,count_even)
exit(0)
}
function is_prime(x, i) {
if (x <= 1) {
return(0)
}
for (i=2; i<=int(sqrt(x)); i++) {
if (x % i == 0) {
return(0)
}
}
return(1)
}
</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
Primes which contain only one odd number 1-999: 45

2 23 29 41 43 47 61 67 83 89
101 103 107 109 127 149 163 167 181 211
233 239 251 257 271 277 293 307 347 349
367 383 389 419 431 433 439 457 479 491
499 503 509 521 523 541 547 563 569 587
613 617 619 631 653 659 673 677 691 701
709 727 743 761 769 787 811 839 853 857
859 877 907 929 941 947 967 983
Primes which contain only one even number 1-999: 78
</pre>
</pre>