Autor Beitrag
Mosh-Josh
Hält's aus hier
Beiträge: 1

Win XP

BeitragVerfasst: So 26.02.06 14:31 
Da ich nächste Woche Klausur schreibe und wir erst seit wenigen Wochen mit Delphi 6.0 arbeiten, hat uns unser Lehrer ein Arbeitsblatt gegeben. Eine Aufgabe lautet: Erstelle eine Konsolenanwendung, die Primzahlen erkennt wenn du sie eingibst!
Ich habe ein bisschen herumprobiert, allerdings gibt die Konsole bei jeder ungeraden Zahl die Meldung "Primzahl" und bei jeder geraden Zahl "Keine Primzahl" aus. Kann mir jemand helfen?


Hier der Quelltext:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
var
zahl,teiler:integer;
primzahl:boolean;

begin
  primzahl:=true; teiler:=2;
  writeln('Bitte geben sie eine Zahl ein!');
  readln(zahl);
 Repeat
   if zahl mod teiler = 0 then
   primzahl:=false; teiler:=teiler+1
 until not primzahl or primzahl;
  if primzahl then writeln('Die Zahl ist eine Primzahl!');
  if not primzahl then writeln('Die Zahl ist keine Primzahl!');
 readln;

end.


Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt
Moderiert von user profile iconChristian S.: Topic aus Delphi Language (Object-Pascal) / CLX verschoben am So 26.02.2006 um 13:36
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: So 26.02.06 14:38 

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

Win XP, Win 2000, Win 98, Linux
D4 Prof
BeitragVerfasst: So 26.02.06 17:05 
Hallo
in Zeile 12 steht bei dir
ausblenden Delphi-Quelltext
1:
 until not primzahl or primzahl;					

Das ist natülich immer wahr. Besser wäre vielleicht
ausblenden Delphi-Quelltext
1:
 until not primzahl or (teiler>=zahl);					

Gruß
Christian

PS: Optimierungen mit Wurzel oder gar Miller-Rabin lasse ich mal lieber aus
Tilo
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 1098
Erhaltene Danke: 13

Win7 geg. WInXP oder sogar Win98
Rad2007
BeitragVerfasst: So 26.02.06 22:43 
statt
ausblenden Quelltext
1:
teiler:=teiler+1;					


ginge auch:
ausblenden Quelltext
1:
inc(teiler);					
DaRkFiRe
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 526

WinXP Home & Professional
C, C++, Delphi
BeitragVerfasst: So 26.02.06 23:43 
Ich werfe mal folgendes ein:

Sei v die zu prüfende Zahl

prim = wahr
Von i = 2 bis v/2:
Wenn (v % i = 0), dann: prim = falsch, Schleife abbrechen

_________________
Lang ist der Weg durch Lehren - kurz und wirksam durch Beispiele! Seneca
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: Mo 27.02.06 00:41 
Es reicht schon bis sqrt(v); (Wurzel von v).

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



BeitragVerfasst: Mi 01.03.06 10:03 
Hier mal eine Funktion aus dem Easy Delphi Helper, dann kannste es mit deinem Algorithmus vergleichen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
Function IsPrim(zahl : Integer): boolean;
var
i: integer;
begin
  result := true;
  If zahl = 1 then
  begin
    result := false;
    exit;
  end;
  For i := 2 to (zahl div 2do
  begin
    If ((zahl mod i) = 0then
    begin
      result := false;
      exit;
    end;
  end;
end;