unit U_TSHirt5;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    SearchUpBtn: TButton;
    DigitsEdt: TEdit;
    Label1: TLabel;
    UpDown1: TUpDown;
    ListBox1: TListBox;
    Label2: TLabel;
    Memo1: TMemo;
    StatusBar1: TStatusBar;
    procedure SearchBtnClick(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function isprime(n:integer):boolean;
var i,j,stop:integer;
begin
  result:=false;
  {get rid of most of the cases right away}
  if (n mod 2 = 0) or (n mod 3=0) or (n mod 5 =0) then exit;
  result:=true;
  i:=7;
  stop:=trunc(sqrt(n+0.0));
  while result and (i<=stop) do
  begin
    if i*(n div i)=n then result:=false;
    inc(i);
  end;
end;

{********* Reverse ********}
function reverse(n:integer):integer;
{reverse the digits of an integer}
begin
  result:=0;
  while n>0 do
  begin
    result:=10*result+n mod 10;
    n:=n div 10;
  end;
end;

function intpower(n,power:integer):integer;
var i:integer;
begin
  result:=1;
  for i:=1 to power do result:=result*n;
end;

procedure TForm1.SearchBtnClick(Sender: TObject);
var stop,i,r,count:integer;
begin
  listbox1.clear;
  i:=intpower(10,updown1.position-1);
  stop:=i*10-1;
  count:=0;
  while (i<=stop) and (count<=100) do
  begin
    r:=reverse(i);
    if isprime(i) and isprime(r) and (i<>r) then
    begin
      inc(count);
      listbox1.items.add(inttostr(i) + ' is an emirp');
    end;
    inc(i);
  end;
end;

end.