Prime numbers which contain 123: Difference between revisions

Added Algol 68
m (added prime numbers to the task's category.)
(Added Algol 68)
Line 8:
As above, but only show the <u>count</u> of those primes &nbsp; '''n''' &nbsp; that contain the (above) string, &nbsp; where &nbsp; '''n &nbsp; &lt; &nbsp; 1,000,000'''.
<br><br>
 
=={{header|ALGOL 68}}==
<lang algol68>BEGIN # find primes whose decimal representation contains 123 #
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;
# find the appropriate primes #
# as observed by the Wren sample, the primes must have a least 4 digits #
INT show max = 100 000;
INT p123 count := 0;
FOR n FROM 1001 TO UPB prime DO
IF prime[ n ] THEN
# have a prime #
BOOL has 123 := FALSE;
INT v := n;
WHILE v >= 123 AND NOT ( has 123 := v MOD 1000 = 123 ) DO
v OVERAB 10
OD;
IF has 123 THEN
# the prime contains "123" #
p123 count +:= 1;
IF n <= show max THEN
print( ( whole( n, -7 ) ) );
IF p123 count MOD 12 = 0 THEN print( ( newline ) ) FI
FI
FI
FI;
IF n = 100 000 THEN
print( ( newline, "Found ", whole( p123 count, 0 ), " ""123"" primes below ", whole( show max, 0 ), newline ) )
FI
OD;
print( ( newline, "Found ", whole( p123 count, 0 ), " ""123"" primes below ", whole( UPB prime, 0 ), newline ) )
END</lang>
{{out}}
<pre>
1123 1231 1237 8123 11239 12301 12323 12329 12343 12347 12373 12377
12379 12391 17123 20123 22123 28123 29123 31123 31231 31237 34123 37123
40123 41231 41233 44123 47123 49123 50123 51239 56123 59123 61231 64123
65123 70123 71233 71237 76123 81233 81239 89123 91237 98123
Found 46 "123" primes below 100000
 
Found 451 "123" primes below 1000000
</pre>
 
=={{header|Go}}==
3,032

edits