Alguém ajuda, Lista de números primos em delphi

Delphi

23/01/2018

Eu estava tentando fazer um programa em delphi que lista os números primos de 1 até 1000, mas por algum motivo ele faz. Se alguém puder olhar para mim, eu fico grato
procedure TForm1.Button1Click(Sender: TObject);
begin
      Application.CreateForm(TForm2,Form2);
      Form2.ProgressBar1.Max:= 1000;
      Form2.ProgressBar2.Max:=1000;
      Form2.Show;
      for n1 := 1 to 1000 do
        begin
          for n2 := 1 to 1000 do
            begin
              if (n1 mod n2)=0 then
              begin
                c:=c+1;
              end;
              Form2.ProgressBar2.Position:=n2;
            end;
            Form2.ProgressBar1.Position:=n1;
            if c=2 then
            begin
              Memo1.Text:= Memo1.Text + inttostr(n1) + chr(13)+chr(10);
            end;
        end;
        Form2.Close;
end;
Gabriel

Gabriel

Curtidas 0

Melhor post

Natanael Ferreira

Natanael Ferreira

24/01/2018

Teste esta função:

function IsPrimo(n: integer): boolean;
var
  i, p: integer;
begin
  Result := False;
  p := 0;
  for i := 1 to n do
    if (n mod i) = 0 then
      Inc(p);
  if p = 2 then
    Result := True;
end;

Exemplo de uso:

var
  n1: integer;
begin
  for n1 := 1 to 1000 do
    if IsPrimo(n1) then
      Memo1.Text := Memo1.Text + inttostr(n1) + chr(13) + chr(10);
end;
GOSTEI 1

Mais Respostas

Luiz Vichiatto

Luiz Vichiatto

23/01/2018

Gabriel

Para que isto funcione primeiramente leia a definição de números primos, https://pt.wikipedia.org/wiki/N%C3%BAmero_primo
https://www.matematica.pt/faq/numero-primo.php

E utilizar o crivo de Eratóstenes.
GOSTEI 0
Gabriel

Gabriel

23/01/2018

Teste esta função:

function IsPrimo(n: integer): boolean;
var
  i, p: integer;
begin
  Result := False;
  p := 0;
  for i := 1 to n do
    if (n mod i) = 0 then
      Inc(p);
  if p = 2 then
    Result := True;
end;

Exemplo de uso:

var
  n1: integer;
begin
  for n1 := 1 to 1000 do
    if IsPrimo(n1) then
      Memo1.Text := Memo1.Text + inttostr(n1) + chr(13) + chr(10);
end;

o seu p é meu C, e eu vi que eu tinha esquecido de zerar ele antes do segundo for
GOSTEI 0
POSTAR