Concatenate two primes is also prime: Difference between revisions

m
(added Arturo)
m (→‎{{header|Wren}}: Minor tidy)
 
(9 intermediate revisions by 6 users not shown)
Line 471:
end sub</syntaxhighlight>
 
 
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <locale.h>
 
bool isPrime(int n) {
if (n < 2) return false;
if (n%2 == 0) return n == 2;
if (n%3 == 0) return n == 3;
int d = 5;
while (d*d <= n) {
if (n%d == 0) return false;
d += 2;
if (n%d == 0) return false;
d += 4;
}
return true;
}
 
int compare(const void* a, const void* b) {
int arg1 = *(const int*)a;
int arg2 = *(const int*)b;
if (arg1 < arg2) return -1;
if (arg1 > arg2) return 1;
return 0;
}
 
int main() {
int primes[30] = {2}, results[200];
int i, j, p, q, pq, limit = 100, pc = 1, rc = 0;
for (i = 3; i < limit; i += 2) {
if (isPrime(i)) primes[pc++] = i;
}
for (i = 0; i < pc; ++i) {
p = primes[i];
for (j = 0; j < pc; ++j) {
q = primes[j];
pq = (q < 10) ? p * 10 + q : p * 100 + q;
if (isPrime(pq)) results[rc++] = pq;
}
}
qsort(results, rc, sizeof(int), compare);
setlocale(LC_NUMERIC, "");
printf("Two primes under 100 concatenated together to form another prime:\n");
for (i = 0, j = 0; i < rc; ++i) {
if (i > 0 && results[i] == results[i-1]) continue;
printf("%'6d ", results[i]);
if (++j % 10 == 0) printf("\n");
}
printf("\n\nFound %d such concatenated primes.\n", j);
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
Two primes under 100 concatenated together to form another prime:
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 1,117 1,123
1,129 1,153 1,171 1,319 1,361 1,367 1,373 1,723 1,741 1,747
1,753 1,759 1,783 1,789 1,913 1,931 1,973 1,979 1,997 2,311
2,341 2,347 2,371 2,383 2,389 2,917 2,953 2,971 3,119 3,137
3,167 3,719 3,761 3,767 3,779 3,797 4,111 4,129 4,153 4,159
4,337 4,373 4,397 4,723 4,729 4,759 4,783 4,789 5,323 5,347
5,923 5,953 6,113 6,131 6,143 6,173 6,197 6,719 6,737 6,761
6,779 7,129 7,159 7,331 7,919 7,937 8,311 8,317 8,329 8,353
8,389 8,923 8,929 8,941 8,971 9,719 9,743 9,767
 
Found 128 such concatenated primes.
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
function Compare(P1,P2: pointer): integer;
{Compare for quick sort}
begin
Result:=Integer(P1)-Integer(P2);
end;
 
procedure ConcatonatePrimes(Memo: TMemo);
{Show concatonated pairs of primes that are also prime}
var List: TList;
var I,P1,P2,ConCat: integer;
var Sieve: TPrimeSieve;
const Max =100;
var S: string;
 
function ConcatNums(I1,I2: integer): integer;
begin
Result:=StrToInt(IntToStr(I1)+IntToStr(I2));
end;
 
begin
{Create sieve to for fast prime generation}
Sieve:=TPrimeSieve.Create;
try
List:=TList.Create;
try
{Sieve first 1,000 primes}
Sieve.Intialize(1000);
 
{Generate all combinations of primes}
{ P1 and P2, from 2 to 100}
P1:=2;
while P1<Max do
begin
P2:=2;
while P2<Max do
begin
{Concatonates the two primes}
ConCat:=ConcatNums(P1,P2);
{Test if it is prime and only store unique primes}
if IsPrime(ConCat) then
if List.IndexOf(Pointer(ConCat))<0 then
List.Add(Pointer(ConCat));
P2:=Sieve.NextPrime(P2);
end;
P1:=Sieve.NextPrime(P1);
end;
{Sort list in numerical order}
List.Sort(Compare);
{Display the result}
Memo.Lines.Add('Concatonated Primes Found: '+IntToStr(List.Count));
for I:=0 to List.Count-1 do
begin
S:=S+Format('%5d',[integer(List[I])]);
if (I mod 10)=9 then S:=S+CRLF;
end;
Memo.Lines.Add(S);
finally List.Free; end;
finally Sieve.Free; end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
Concatonated Primes Found: 128
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
Elapsed Time: 3.990 ms.
 
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc sort . d[] .
for i = 1 to len d[] - 1
for j = i + 1 to len d[]
if d[j] < d[i]
swap d[j] d[i]
.
.
.
.
func isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
for i = 2 to 99
if isprim i = 1
prims[] &= i
.
.
for p1 in prims[]
for p2 in prims[]
h$ = p1 & p2
h = number h$
if isprim h = 1
r[] &= h
.
.
.
sort r[]
print r[]
</syntaxhighlight>
 
=={{header|Factor}}==
Line 604 ⟶ 807:
[8311,8317,8329,8353,8389,8923,8929,8941,8971,9719,9743,9767]
</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j"> concat=. (] + (* 10 ^ #@":))"1 0
 
_12 ]\ /:~ (#~ 1&p:) , concat~ i.&.(p:inv) 100
23 37 53 73 113 137 173 193 197 211 223 229
233 241 271 283 293 311 313 313 317 317 331 337
347 353 359 367 373 373 379 383 389 397 433 523
541 547 571 593 613 617 673 677 719 733 743 761
773 797 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</syntaxhighlight>
 
=={{header|jq}}==
Line 697 ⟶ 916:
{{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}</pre>
 
=={{header|Lua}}==
Based on the Algol W sample.
<syntaxhighlight lang="lua">
do -- find primes whose decimal representation is the concatenation of 2 primes < 100
local MAX_PRIME = 99 * 99
-- returns true if n is prime, false otherwise, uses trial division
local function isPrime ( n )
if n < 3 then return n == 2
elseif n % 3 == 0 then return n == 3
elseif n % 2 == 0 then return false
else
local prime = true
local f, f2, toNext = 5, 25, 24
while f2 <= n and prime do
prime = n % f ~= 0
f = f + 2
f2 = toNext
toNext = toNext + 8
end
return prime
end
end
local concatPrime = {}
-- tables of small primes, sp2 will be the final digits so does not include 2 or 5
local sp1 = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37
, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
}
local sp2 = { 3, 7, 11, 13, 17, 19, 23, 29, 31, 37
, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
}
-- find the concatenated primes
for i = 1, MAX_PRIME do concatPrime[ i ] = false end
for _, p1 in pairs( sp1 ) do
for _, p2 in pairs( sp2 ) do
local pc = ( p1 * ( p2 < 10 and 10 or 100 ) ) + p2
concatPrime[ pc ] = isPrime( pc )
end
end
-- print the concatenated primes
local cCount = 0
for i = 1, MAX_PRIME do
if concatPrime[ i ] then
io.write( string.format( "%5d", i ) )
cCount = cCount + 1
if cCount % 10 == 0 then io.write( "\n" ) end
end
end
io.write( "\n\nFound ", cCount, " concat primes" )
end
</syntaxhighlight>
{{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|Nim}}==
Line 787 ⟶ 1,075:
Found 128 such primes: {23,37,53,73,113,"...",8941,8971,9719,9743,9767}
</pre>
 
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<syntaxhighlight lang="plm">
100H: /* FIND SOME PAIRS OF PRIMES BETWEEN 1 AND 99 SUCH THAT IF THEIR */
/* DIGITS ARE CONCATENATED, THE RESULT IS ALSO A PRIME */
 
/* CP/M BDOS SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$STRING( .( 0DH, 0AH, '$' ) ); END;
PR$NUMBER: PROCEDURE( N );
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR( 6 ) BYTE, W BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;
 
/* TASK */
 
DECLARE FALSE LITERALLY '0';
DECLARE TRUE LITERALLY '0FFH';
DECLARE CONCAT$PRIME LITERALLY '0FH';
DECLARE MAX$LOW$PRIME LITERALLY '99';
DECLARE PRIME ( 10$000 )BYTE; /* PRIME SIEVE. A BIT LARGER THN NEEDED */
/* THE FIRST NYBBLE WILL BE SET TO 0 IF */
/* IT IS A CONCATENATED PRIME */
/* THE SIZE OF PRIME SHOULD BE AT LEAST MAX$LOW$PRIME SQUARED */
/* SIEVE THE PRIMES TO MAX$PRIME */
DECLARE ( I, J, COUNT ) ADDRESS;
PRIME( 1 ) = FALSE; PRIME( 2 ) = TRUE;
DO I = 3 TO LAST( PRIME ) BY 2; PRIME( I ) = TRUE; END;
DO I = 4 TO LAST( PRIME ) BY 2; PRIME( I ) = FALSE; END;
DO I = 3 TO MAX$LOW$PRIME + 1;
IF PRIME( I ) THEN DO;
DO J = I * I TO LAST( PRIME ) BY I + I; PRIME( J ) = FALSE; END;
END;
END;
/* FIND THE CONCATEDNATED PRIMES */
COUNT = 0;
DO I = 2 TO MAX$LOW$PRIME;
IF PRIME( I ) THEN DO;
DO J = 2 TO MAX$LOW$PRIME;
IF PRIME( J ) THEN DO;
DECLARE CP ADDRESS;
IF J < 10 THEN CP = I * 10;
ELSE CP = I * 100;
CP = CP + J;
IF PRIME( I ) AND PRIME( J ) AND PRIME( CP ) THEN DO;
/* CP IS A CONCATENATED PRIME - FLAG PRIME( CP ) AS SUCH */
PRIME( CP ) = CONCAT$PRIME;
END;
END;
END;
END;
END;
/* SHOW THE CONCATENATED PRIMES */
/* SINGLE DIGIT NUMBERS CAN'T BE CONCATENATED PRIMES, START AT 10 */
DO I = 3 TO LAST( PRIME ) BY 2;
IF PRIME( I ) = CONCAT$PRIME THEN DO;
/* HAVE A CONCATENATED PRIME */
CALL PR$CHAR( ' ' );
IF I < 1000 THEN DO;
CALL PR$CHAR( ' ' );
IF I < 100 THEN CALL PR$CHAR( ' ' );
END;
CALL PR$NUMBER( I );
IF ( COUNT := COUNT + 1 ) MOD 10 = 0 THEN CALL PR$NL;
END;
END;
 
EOF
</syntaxhighlight>
{{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
</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">from itertools import takewhile
 
def is_prime(x):
return x > 1 and all(x % d for d in takewhile(lambda n: n * n <= x, primes))
 
def init_primes(n):
global primes
primes = [2]
for x in range(3, n + 1, 2):
if is_prime(x): primes.append(x)
 
def concat(x, y):
return 10 ** len(str(y)) * x + y
 
init_primes(99)
print(*sorted(n for x in primes for y in primes if is_prime(n := concat(x, y))))</syntaxhighlight>
{{out}}
<pre>23 37 53 73 113 137 173 193 197 211 223 229 233 241 271 283 293 311 313 313 317 317 331 337 347 353 359 367 373 373 379 383 389 397 433 523 541 547 571 593 613 617 673 677 719 733 743 761 773 797 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</pre>
 
=={{header|Quackery}}==
Line 1,014 ⟶ 1,418:
Found 128 prime numbers
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
≪ { } 2
'''DO'''
3
'''DO'''
OVER →STR OVER + STR→
'''IF''' DUP ISPRIME? '''THEN'''
4 ROLL SWAP
'''IF''' DUP2 POS NOT '''THEN''' + '''ELSE''' DROP '''END'''
UNROT
'''ELSE''' DROP '''END'''
NEXTPRIME
'''UNTIL''' DUP 100 > '''END'''
DROP NEXTPRIME
'''UNTIL''' DUP 100 > '''END'''
SORT
≫ '<span style="color:blue">P1P2</span>' STO
 
{{out}}
<pre>
1: { 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 }
</pre>
 
Line 1,041 ⟶ 1,469:
8389 8923 8929 8941 8971 9719 9743 9767
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var upto = 100
Line 1,082 ⟶ 1,511:
{{libheader|Wren-fmt}}
{{libheader|Wren-seq}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
import "./seq" for Lst
 
var limit = 99
Line 1,098 ⟶ 1,527:
results.sort()
System.print("Two primes under 100 concatenated together to form another prime:")
Fmt.tprint("$,6d", results, 10)
for (chunk in Lst.chunks(results, 10)) Fmt.print("$,6d", chunk)
System.print("\nFound %(results.count) such concatenated primes.")</syntaxhighlight>
 
9,476

edits