Autor Beitrag
luckyluc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 103



BeitragVerfasst: Di 20.01.09 18:56 
Hallo
Ich soll diesen Code:
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:
procedure TForm1.Button1Click(Sender: TObject);
begin
zeile:= 0;
eing_zahl:= StrToInt (Edit1.Text);


for i := 4 to eing_zahl do
begin
 primzahl :=true;
 teiler := 2;

   repeat
   if i mod teiler = 0 then primzahl := false;
   inc (teiler);
   until teiler >=Trunc(sqrt(i));

   if primzahl = true then
   begin
   inc (zeile);
    StringGrid1.Cells[0,zeile] := IntToStr(i);
    StringGrid1.RowCount := StringGrid1.RowCount+1;
    end;

end;//for


Also diesen Code soll ich in eine repeat-schleife umsetzten (also die äußere for-schleife als äußere repeat schleife schreiben). Zudem soll ich i jeweils um 2 erhöhen. (hierbei handelt es sich um ein programm dass dir alle primzahlen bis zu einem gewissen bereich ausgibt; deshalb i um 2 erhöhen, also damit man nur ungerade zahlen prüft)
ich habe das folgendermaßen umgesetzt:
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:
procedure TForm1.Button1Click(Sender: TObject);
begin
zeile:= 0;
eing_zahl:= StrToInt (Edit1.Text);
i:= 3;

repeat
i:= i+2// hier wird i um jewils 2 erhöht , damit nur ungerade zahlen geprüft werden.
primzahl:=true;
teiler := 2;

   repeat
   if i mod teiler = 0 then primzahl := false;
   inc (teiler);
   until teiler >= i-1;
  
until i = eing_zahl;

   if primzahl = true then
   begin
   inc (zeile);
    StringGrid1.Cells[0,zeile] := IntToStr(i);
    StringGrid1.RowCount := StringGrid1.RowCount+1;
    end;


Dieser Code fukt. nicht, ich hab irgenwie ne endlosschleife gebaut. könnt ihr mir bitte helfen?
DANKE!


Moderiert von user profile iconNarses: Topic aus Sonstiges (Delphi) verschoben am Di 20.01.2009 um 17:58
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Di 20.01.09 19:00 
was passiert wen eing_zahl gerade ist? dann ist i nie eing_zahl sondern entweder eing_zahl -1 oder eing_zahl +1.

deswegen:
ausblenden Delphi-Quelltext
1:
until i > eing_zahl;					


lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
luckyluc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 103



BeitragVerfasst: Di 20.01.09 19:04 
ja danke für den tipp, das programm hängt sich jetzt nicht mehr auf, gibt aber auch keine primzahlen aus. da müssen sonst noch fehler drin sein. ich find den aber einfach nicht.
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Di 20.01.09 19:31 
der teil:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
if primzahl = true then
   begin
   inc (zeile);
    StringGrid1.Cells[0,zeile] := IntToStr(i);
    StringGrid1.RowCount := StringGrid1.RowCount+1;
    end;


gehört da rein:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
repeat
   if i mod teiler = 0 then primzahl := false;
   inc (teiler);
   until teiler >= i-1;


lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
luckyluc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 103



BeitragVerfasst: Di 20.01.09 19:41 
danke, jedoch zeigt der mir jetzt auch zahlen an, die gar keine primzahlen sind, bzw. er zeigt verschieden zahllen mehrfach an. da is noch irgendwo en fehler, vesrteh ich aber einfach net.
luckyluc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 103



BeitragVerfasst: Di 20.01.09 20:06 
ah, jetzt hab ichs . danke dir für deine hilfe hab den teil den du mir nanntest zwischen die zwei "untils" gesetzt und funkt Danke!
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Di 20.01.09 20:08 
jep, bin grad draufgekommen, sorry wegen dem fehler.

hier mal der fertige source für alle die es interessiert:
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:
procedure TForm1.Button1Click(Sender: TObject);
begin
  zeile:= 0;
  eing_zahl:= StrToInt (Edit1.Text);
  i:= 3;

  repeat
    i:= i+2// hier wird i um jewils 2 erhöht , damit nur ungerade zahlen geprüft werden.
    primzahl:=true;
    teiler := 2;

    repeat
      if i mod teiler = 0 then 
        primzahl := false;
      inc (teiler);
    until teiler >= i-1;
    if primzahl = true then begin
      inc (zeile);
      StringGrid1.Cells[0,zeile] := IntToStr(i);
      StringGrid1.RowCount := StringGrid1.RowCount+1;
    end;
  until i > eing_zahl;
end;


lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
DeddyH
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Di 20.01.09 20:34 
Ein paar kleine Änderungen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
procedure TForm1.Button1Click(Sender: TObject);
begin
  zeile:= 0;
  eing_zahl:= StrToInt (Edit1.Text);
  i:= 3;

  repeat
    inc(i,2); // hier wird i um jeweils 2 erhöht , damit nur ungerade zahlen geprüft werden.
    teiler := 2;

    repeat
      primzahl := i mod teiler <> 0;
      inc (teiler);
    until (teiler >= i-1or not primzahl; //wenn wir es schon wissen, müssen wir nicht weitermachen
    if primzahl{ = true} then begin  //niemals auf true abfragen, das kann böse enden
      inc (zeile);
      StringGrid1.Cells[0,zeile] := IntToStr(i);
      StringGrid1.RowCount := StringGrid1.RowCount+1;
    end;
  until i > eing_zahl;
end;