Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to find two prime numbers that, when concatenated, form another prime number in Pascal

1 Answer

0 votes
program PrimeConcat;

uses
  SysUtils; { for IntToStr and StrToInt }

const
  Limit = 12; { you can increase this }
  MaxPrimes = 100;

var
  Primes: array[1..MaxPrimes] of Integer;
  PrimeCount: Integer;

function IsPrime(n: LongInt): Boolean;
var
  i: LongInt;
begin
  if n < 2 then
    IsPrime := False
  else if (n mod 2 = 0) and (n <> 2) then
    IsPrime := False
  else
  begin
    i := 3;
    while i * i <= n do
    begin
      if n mod i = 0 then
      begin
        IsPrime := False;
        Exit;
      end;
      i := i + 2;
    end;
    IsPrime := True;
  end;
end;

function ConcatInts(a, b: Integer): LongInt;
var
  s: String;
begin
  s := IntToStr(a) + IntToStr(b);
  ConcatInts := StrToInt(s);
end;

var
  i, j: Integer;
  num: LongInt;

begin
  PrimeCount := 0;

  { Generate primes up to limit }
  for i := 2 to Limit do
    if IsPrime(i) then
    begin
      Inc(PrimeCount);
      Primes[PrimeCount] := i;
    end;

  { Check pairs }
  for i := 1 to PrimeCount do
    for j := 1 to PrimeCount do
    begin
      if i = j then
        Continue;
      num := ConcatInts(Primes[i], Primes[j]);
      if IsPrime(num) then
        WriteLn(Primes[i], ' + ', Primes[j], ' -> ', num, ' is prime');
    end;
end.



(*
run:

2 + 3 -> 23 is prime
2 + 11 -> 211 is prime
3 + 7 -> 37 is prime
3 + 11 -> 311 is prime
5 + 3 -> 53 is prime
7 + 3 -> 73 is prime
11 + 3 -> 113 is prime

*)



answered Nov 28, 2025 by avibootz
...