Primes which contain only one odd digit: Difference between revisions

Added Algol 68
(Added Algol 68)
Line 8:
Show on this page only the &nbsp; <u>count</u> &nbsp; of those primes under &nbsp; '''1,000,000''' &nbsp; which when expressed in decimal contains only one odd digit.
<br><br>
 
=={{header|ALGOL 68}}==
<lang algol68>BEGIN # find primes whose decimal representation contains only one odd digit #
INT max prime = 1 000 000;
# sieve the primes to max prime #
[ 1 : max prime ]BOOL prime;
prime[ 1 ] := FALSE; prime[ 2 ] := TRUE;
FOR i FROM 3 BY 2 TO UPB prime DO prime[ i ] := TRUE OD;
FOR i FROM 4 BY 2 TO UPB prime DO prime[ i ] := FALSE OD;
FOR i FROM 3 BY 2 TO ENTIER sqrt( UPB prime ) DO
IF prime[ i ] THEN FOR s FROM i * i BY i + i TO UPB prime DO prime[ s ] := FALSE OD FI
OD;
# show a count of primes #
PROC show total = ( INT count, INT limit, STRING name )VOID:
print( ( newline, "Found ", whole( count, 0 ), " ", name, " primes upto ", whole( limit, 0 ), newline ) );
# find the appropriate primes #
# 2 is the only even prime, so the final digit must be odd for primes > 2 #
# so we only need to check that the digits other than the last are all even #
INT show max = 1 000;
INT p1odd count := 0;
FOR n FROM 3 TO UPB prime DO
IF prime[ n ] THEN
BOOL p1odd := TRUE;
INT v := n OVER 10;
WHILE v > 0 AND ( p1odd := NOT ODD v ) DO
v OVERAB 10
OD;
IF p1odd THEN
# the prime has only 1 odd digit #
p1odd count +:= 1;
IF n <= show max THEN
print( ( whole( n, -5 ) ) );
IF p1odd count MOD 12 = 0 THEN print( ( newline ) ) FI
FI
FI
FI;
IF n = show max THEN
print( ( newline ) );
show total( p1odd count, show max, "single-odd-digit" )
FI
OD;
show total( p1odd count, UPB prime, "single-odd-digit" )
END</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
 
Found 45 single-odd-digit primes upto 1000
 
Found 2560 single-odd-digit primes upto 1000000
</pre>
 
=={{header|F_Sharp|F#}}==
Line 19 ⟶ 73:
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
</pre>
 
=={{header|Go}}==
{{trans|Wren}}
3,038

edits