Autor Beitrag
Harald Enslen
Hält's aus hier
Beiträge: 5



BeitragVerfasst: Fr 03.12.04 15:55 
hi,

durch den Thread mit den Primzahlen bin ich angeregt worden, auch ein solches Programm zu schreiben. Wenn ich mein Programm aber starte zeigt es alle Zahlen bis 'max' und eine unsinnige Zahl von Primzahlen an.

Die Funktion:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
function isprim(zahl:integer):boolean;
var teiler:integer;
    wurzel:extended;
begin
teiler:=1;
result:=false;
wurzel:=sqrt(zahl);

while ((teiler<=wurzel) and (teiler<>(zahl-1)) and result) do
      begin
      if (odd(zahl) or (zahl=2)) then
        begin
        Inc(teiler);
        if ((zahl mod teiler)=0) then
         result:=true;
        end
      else
        result:=true;
      end;
end;


Der Programmcode:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
procedure TfrmMain.BitBtn5Click(Sender: TObject);
var bis, i, zaehler: Cardinal;

begin
  if lbledtprim.Text='' then
    ShowMessage('Bitte geben Sie einen Wert ein!')
  else if strtoint(lbledtprim.Text)>=4294967295 then
    ShowMessage('Die eingegebene Zahl ist zu groß!')
  else
    try
      bis:=strtoint(lbledtprim.Text);
    except
      ShowMessage('Bitte geben Sie einen gültigen Wert ein!');
    end;

    if lbledtprim.Text<>'' then
      begin
        memprim.Text:='';
        for i:=2 to bis do
          begin
            if isprim(i)=false then
              begin
                Inc(zaehler);
                if memprim.Text='' then
                  memprim.Text:=IntToStr(i)
                else
                  memprim.Text:=memprim.Text+', '+IntToStr(i);
              end;
          end;

        ShowMessage('gefundenen Primzahlen: '+ IntToStr(zaehler));
      end;
end;
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Fr 03.12.04 17:09 
Erst mal ein bisschen optimieren:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
function isprim(Zahl: Integer): Boolean;
var 
  Teiler: Integer;
  Wurzel: Extended;
begin
  if not odd(Zahl) AND (Zahl <> 2) then
  begin
    Result := False;
    Exit;
  end;

  Teiler := 2;
  Wurzel := sqrt(zahl);

  while Teiler <= Wurzel do
  begin
    if Zahl mod Teiler = 0  then
    begin
      Result := False;
      Exit;
    end;
    Inc(Teiler);
  end;
end;


Und schon funktionierts.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Fr 03.12.04 18:16 
GTA-Place hat folgendes geschrieben:
Erst mal ein bisschen optimieren:

Es geht sogar noch paar Zeilen schneller...

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
function isprim(Zahl: Integer): Boolean;
var 
  Teiler: Integer;
  Wurzel: Integer;
begin
  Result := False;

  if not odd(Zahl) AND (Zahl <> 2) then
    Exit;

  Teiler := 3;
  Wurzel := Trunc(Sqrt(Zahl));

  while Teiler <= Wurzel do
  begin
    if Zahl mod Teiler = 0 then
      Exit;
    Inc(Teiler, 2);
  end;

  Result := True;
end;


@Mods: Bitte diesen Thread unter Optimierung und Algos einordnen, evtl. mergen.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
thepaul
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 29

Win XP
D7 Pers.
BeitragVerfasst: Sa 04.12.04 10:41 
hi,

ich hab das jetzt auch mal versucht. wenn man große Zahlen eingibt rechnet er nur ganzschön lang. Wie baut man denn da so einen Fortschrittsbalken (ich glaub das heißt in Delphi 'ProgressBar') ein?
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Sa 04.12.04 10:51 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.Button1Click(Sender: TObject);
var
  X: Integer;
begin
  ProgressBar1.Max := 100000 div 4;  // Am besten Teilen, sonst dauerts länger

  for X := 1 to 100000 do
    ProgressBar1.Position := Round(ProgressBar1.Max - (100000 div 4-X));
end;

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
thepaul
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 29

Win XP
D7 Pers.
BeitragVerfasst: Sa 04.12.04 14:24 
alles klar!

jetzt muss ich das nurnoch in mein Prog einbauen!
Harald Enslen Threadstarter
Hält's aus hier
Beiträge: 5



BeitragVerfasst: Sa 04.12.04 15:09 
OK,ich hatte auch noch einen anderen Fehler in meinem Programm! Jetzt klappt aber alles
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 04.12.04 15:13 
Manchmal frage ich mich, ob der Hinweis, das Topic als erledigt zu markieren, doch noch zu klein ist. :gruebel:

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".