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,844 questions

51,765 answers

573 users

How to check if a number can be made prime by deleting a single digit in Pascal

1 Answer

0 votes
program RemoveDigitPrime;

uses
  SysUtils;

function RemoveTheNDigit(num: LongInt; N: Integer): LongInt;
var
  s: string;
begin
  s := IntToStr(num);
  Delete(s, N + 1, 1);   { Pascal strings are 1‑based }
  RemoveTheNDigit := StrToInt(s);
end;

function IsPrime(n: LongInt): Boolean;
var
  i, limit: LongInt;
begin
  if (n < 2) or ((n mod 2 = 0) and (n <> 2)) then
  begin
    IsPrime := False;
    Exit;
  end;

  limit := Trunc(Sqrt(n));
  i := 3;
  while i <= limit do
  begin
    if n mod i = 0 then
    begin
      IsPrime := False;
      Exit;
    end;
    i := i + 2;
  end;

  IsPrime := True;
end;

var
  n, totalDigits, i, tmp: LongInt;
  s: string;

begin
  n := 78919;
  s := IntToStr(n);
  totalDigits := Length(s);

  for i := 0 to totalDigits - 1 do
  begin
    tmp := RemoveTheNDigit(n, i);
    if IsPrime(tmp) then
    begin
      WriteLn('yes number = ', tmp);
      Break;
    end;
  end;
end.




(*
run:

yes number = 7919

*)


 



answered Jan 7 by avibootz

Related questions

...