Concatenate two primes is also prime: Difference between revisions

Added Algol 68
(Added XPL0 example.)
(Added Algol 68)
Line 4:
Find and show here when the concatenation of two primes &nbsp; ('''p<sub>1</sub>''', &nbsp;'''p<sub>2</sub>''') &nbsp; shown in base ten is also prime, &nbsp; where &nbsp; '''p<sub>1</sub>, &nbsp; p<sub>2</sub> &nbsp;&lt;&nbsp; 100'''.
<br><br>
 
=={{header|ALGOL 68}}==
<lang algol68>BEGIN # find primes whose decimal representation is the concatenation of 2 primes #
INT max low prime = 99; # for the task, only need component primes up to 99 #
INT max prime = max low prime * max low prime;
# 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;
# construct a list of the primes up to the maximum low prime to consider #
[ 1 : max low prime ]INT low prime;
INT low pos := 0;
FOR i WHILE low pos < UPB low prime DO IF prime[ i ] THEN low prime[ low pos +:= 1 ] := i FI OD;
# find the primes that can be concatenated to form another prime #
[ 1 : max prime ]BOOL concat prime; FOR i TO UPB concat prime DO concat prime[ i ] := FALSE OD;
# note that all possible concatenated primes have at least 2 digits, so the final digit can't be 2 #
# ( or 5 but we don't check that here ) #
FOR i TO UPB low prime WHILE low prime[ i ] <= max low prime DO
INT p1 = low prime[ i ];
FOR j FROM 2 TO UPB low prime WHILE low prime[ j ] <= max low prime DO
INT p2 = low prime[ j ];
INT pc = ( p1 * IF p2 < 10 THEN 10 ELSE 100 FI ) + p2;
concat prime[ pc ] := prime[ pc ]
OD
OD;
# show the concatenated primes #
INT c count := 0;
FOR n TO UPB concat prime DO
IF concat prime[ n ] THEN
print( ( whole( n, -5 ) ) );
IF ( c count +:= 1 ) MOD 10 = 0 THEN print( ( newline ) ) FI
FI
OD;
print( ( newline, newline, "Found ", whole( c count, 0 ), " concat primes", newline ) )
END</lang>
{{out}}
<pre>
23 37 53 73 113 137 173 193 197 211
223 229 233 241 271 283 293 311 313 317
331 337 347 353 359 367 373 379 383 389
397 433 523 541 547 571 593 613 617 673
677 719 733 743 761 773 797 977 1117 1123
1129 1153 1171 1319 1361 1367 1373 1723 1741 1747
1753 1759 1783 1789 1913 1931 1973 1979 1997 2311
2341 2347 2371 2383 2389 2917 2953 2971 3119 3137
3167 3719 3761 3767 3779 3797 4111 4129 4153 4159
4337 4373 4397 4723 4729 4759 4783 4789 5323 5347
5923 5953 6113 6131 6143 6173 6197 6719 6737 6761
6779 7129 7159 7331 7919 7937 8311 8317 8329 8353
8389 8923 8929 8941 8971 9719 9743 9767
 
Found 128 concat primes
</pre>
 
=={{header|Go}}==
3,022

edits