Autor Beitrag
THF
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 110

Vista
Delphi 2005 professional
BeitragVerfasst: Di 01.03.05 11:02 
Hallo,

gibt es eine Funktion mit der ich gerade und ungerade Zahlen unterscheiden kann.
Eine Procedure soll nur dann ausgeführt werden ,wenn
die Zahl ungerade ist.
Gruß

THF
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Di 01.03.05 11:13 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
if Zahl mod 2 = 0 then
  ShowMessage('Gerade')
else
  ShowMessage('Ungerade');


AXMD
OneOfTen
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Di 01.03.05 11:55 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
if odd(Zahl)
then
  ShowMessage('Ungerade')
else
  ShowMessage('Gerade');
Spaceguide
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Di 01.03.05 15:06 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
if Zahl and 1=1
then
  ShowMessage('Ungerade')
else
  ShowMessage('Gerade');



So, hamma alles? :)
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Di 01.03.05 15:19 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
if boolean(zahl and 1)
then   
  ShowMessage('Ungerade')   
else   
  ShowMessage('Gerade');

würde ich eher nehmen wenn des mit and machst ;>
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Di 01.03.05 15:31 
ausblenden Delphi-Quelltext
1:
2:
3:
if boolean((zahl shl 31shr 31then
  'ungerade' else
  'gerade'


noch ne möglichkeit ;>
retnyg
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2754

SNES, GB, GBA, CPC, A500, 486/66, P4/3.0HT: NintendOS, AmigaOS, DoS
Delphi 5, Delphi 7
BeitragVerfasst: Di 01.03.05 15:33 
wie kommst du auf 31 ?

edit: lol...habs gecheckt ;)
Spaceguide
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Di 01.03.05 15:43 
Hier ganz besonders elegant:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
function IsEven(i : integer) : boolean;
 var s : string;
     c : char;
begin
 s := IntToStr(i);
 c := s[Length(s)];
 Result := (c='0'or (c='2'or (c='4'or (c='6'or (c='8');
end;
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Di 01.03.05 15:54 
wenn wa jetzt schon mit langsamen methoden ankommen.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
function gerade(i: integer): boolean;
begin
  while i >= 1 do dec(i,2);
  result := boolean(i+1);
end;


oder rekursiv
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
function gerade(i: integer): integer;
begin
  if i >= 1 then
    result := gerade(i-2else
    result := i+1;
end;
Harry Hunt
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 50



BeitragVerfasst: Di 01.03.05 16:02 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
function Gerade(Zahl: Integer): Boolean;
var
  Zahlen: array[0..High(Integer)] of Boolean;
  I: Integer;
begin
  for I := 1 to (High(Integer) div 2do
    Zahlen[I * 2] := True;
  Result := Zahlen[abs(Zahl)];
end;


:lol:

EDIT: Mist, das lässt sich noch nicht mal kompilieren...
Spaceguide
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Di 01.03.05 17:04 
Kann noch jemand mit Fliesskommazahlen kommen?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
function IsOdd(i : integer) : boolean; register;
ASM
 AND EAX,1
end;
Spaceguide
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Di 01.03.05 17:08 
Oder hier noch eine schöne Variante, ausgehend von der Annahme, dass Null eine gerade Zahl ist:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
function IsEven(i : integer) : boolean;
begin
 if i=0 then Result:=true else
 Result := not IsEven(Pred(i));
end;
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5284
Erhaltene Danke: 27

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 01.03.05 18:21 
wird das hier ein kampf der codes, man kanns auch echt übertreiben :D

ich glaube

ausblenden Delphi-Quelltext
1:
odd					

ist alles was er wissen wollte :mrgreen:
OneOfTen
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Di 01.03.05 18:55 
und wer hatte diesen geistesblitz? :wink:
Spaceguide
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Di 01.03.05 19:22 
Ist doch lustig. Auf so Fragen wie "gerade und ungerade Zahlen unterscheiden", "wie addiere ich zwei Zahlen" bietet sich doch gerade so eine Parodie an.
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Di 01.03.05 21:54 
Leute, ich will doch auch mal! :)
Wenn ich mich nicht täsche kann man nicht beweisen, dass das hier funktioniert, ausser man probiert alle möglichen Zahlen einfach durch:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
function ungerade(const I : Integer) : Boolean;
Var
 J, A : Integer;
begin
 A := random(abs(I))+1;
 while A >= 2 do
 begin
  B := random(A) and not 1;
  For J := 0 to abs(A) do
   B := B xor (2*random(random(J))+1);
  if arccos(cos(B*pi)) > pi/2 then
   A := A div 2
  else
   A := A*3+1;
 end;
 Result := I = A or I and -2;
end;

Der Thread wird bestimmt bald gelockt...
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Di 01.03.05 22:45 
Kommt schon Leute, wir wollen's nicht übertreiben. Seine Frage ist beantwortet - auch ohne ominöse Funktionen die den Arcuscosinus vom Cosinus berechnen...

AXMD