Friday, April 7, 2017

Prime Number on Delphi

Using Delphi to generate prime number? Okay, there's some code out there with

if (i mod 2 <>0) and (i mod 3 <>0) and (i mod 5 <>0) and (i mod 7 <>0) then i is prime.

Don't use that.

That's just for prime number below 100.

Use this instead.

var i,j:integer;
   prime:boolean;
begin
  for i:=2 to 200 do begin
    prime:=true;
    for j:=2 to i-1 do begin
      if i mod j=0 then prime:=false;
    end;
    if prime=true then memo1.Lines.Append(intToStr(i));
  end;
end;
.