Entwickler-Ecke

Delphi Language (Object-Pascal) / CLX - primzahl, warum macht if nicht, was if machen soll?


heyhow - Do 22.06.06 17:15
Titel: primzahl, warum macht if nicht, was if machen soll?
Hallo,

habe folgendes Problem


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
for i := 1 to zahl-1 do
  begin
    if (zahl mod  i) = 0
    then
      label3.Caption := 'Zahl ist keine Primzahl!';
    end
  end;


Das ist ein Teil meines Sources. Ich möchte prüfen, ob die eingegeben zahl (in dem Falle die Variable zahl) eine Primzahl ist. Allerdings gibt er mit auch aus, dass die zahl eine Primzahl ist, wenn ich z.B. die 7 eingebe.. Das dürfte eigentlich nicht sein.

Wo liegt bei diesem Source der Fehler?


heyhow - Do 22.06.06 17:17
Titel: Sorry
Oh, sorry!

Hab den Fehler schon gefunden :-)

Jede Ganzzahl ist durch 1 Teilbar....


Allesquarks - Do 22.06.06 17:45

Die sieben ist auch tatsächlich eine Primzahl 8) :!:


F34r0fTh3D4rk - Do 22.06.06 18:18
Titel: Re: primzahl, warum macht if nicht, was if machen soll?

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
for i := 2 to zahl-1 do
    if (zahl mod  i) = 0
    then
    begin
      label3.Caption := 'Zahl ist keine Primzahl!'
      exit;
    end;