Autor Beitrag
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 15.03.15 19:29 
Moin!

Sehr schön, damit überprüfst du jetzt mal die Tabelle da oben. :idea: :lupe:

(und wenn du ganz pfiffig bist, dann automatisierst du dir das sogar... 8) wenn nicht, auch nicht schlimm, dann halt immer wieder die Zeile anpassen, Programm starten, Button anklicken, etc.)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 15.03.15 19:33 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
x  | x mod N
------------
0  |0
1  |1
2  |2
3  |3
4  |4
5  |0
6  |1
17 |2
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 15.03.15 19:34 
Moin!

Sehr gut, wo war der Fehler? :lupe:

Erweiterung: was ist mit -1 mod 5?

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 15.03.15 19:36 
das bei 5 mod 5 natürlich kein rest bleiben kann ... :P

da kommt -1 als ergebnis raus
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 15.03.15 19:43 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
das bei 5 mod 5 natürlich kein rest bleiben kann ... :P
:zustimm:

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
da kommt -1 als ergebnis raus
Ja, doof, nicht wahr? :? Sehen wir gleich noch...

Bei der Modulorechnung ist also festzuhalten: x mod N ist immer kleiner als N :idea:

Was ist denn mit diesen Termen: x = (x mod N) = ((x +N) mod N) = ((x +2N) mod N)

Für welche x ergibt sich hier eine wahre Aussage?

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 15.03.15 20:01 
für alle x = N
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 15.03.15 20:08 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
für alle x = N
Nö, für die genau nicht mehr. :lupe: sondern :arrow: für x = 0 .. N-1 :think:

Und jetzt drehen wir mal die Denkrichtung um (und lesen die Terme umgekehrt): Es ist also völlig egal, wieviele N wir auf x addieren, solange wir am Ende ein mod N machen, ist das immer gleich!

Nächster Schritt, hier:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
function TForm1.LebtNachbar(x, y: Integer): Boolean;
begin
  ShowMessageFmt('vorher: x=%d, y=%d', [x, y]); // das hier ist nur zum Testen
  if (x < 0then
    x := x +N;
  if (y < 0then
    y := y +N;
   if (x > (N-1)) then
    x := x -N;
  if (y > (N-1)) then
    y := y -N;
  ShowMessageFmt('nachher: x=%d, y=%d', [x, y]); // das hier ist nur zum Testen
  Result := Feld[x, y];
end;
haben wir uns doch ziemlich einen abgebrochen, um am Ende (Markierung) in den Bereich 0..N-1 zu kommen. Wir hatten erst mit x = -1 zu kämpfen, dann mit x = N, was wieder "zuviel" war. :nixweiss:

Wenn wir also am Ende schreiben:
ausblenden Delphi-Quelltext
1:
Result := Feld[x mod N, y mod N];					
dann können wir von den if-Anweisungen was einsparen. Aber welche? :gruebel: Mach mal einen Vorschlag.

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 15.03.15 20:15 
die beiden wo wir +N rechnen, also die beiden für x<0 brauchen wir nicht mehr
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 15.03.15 20:20 
Moin!

Meinst du das so?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
function TForm1.LebtNachbar(x, y: Integer): Boolean;
begin
  ShowMessageFmt('vorher: x=%d, y=%d', [x, y]); // das hier ist nur zum Testen
//  if (x < 0) then
//    x := x +N;
//  if (y < 0) then
//    y := y +N;
   if (x > (N-1)) then
    x := x -N;
  if (y > (N-1)) then
    y := y -N;
  x := x mod N; // das mod N schon hier rechnen,
  y := y mod N; // damit man das gleich ausgeben kann
  ShowMessageFmt('nachher: x=%d, y=%d', [x, y]); // das hier ist nur zum Testen
  Result := Feld[x, y]; // hier wieder rausgenommen und nach oben verschoben
end;


Dann mach mal in den Test-Button wieder einen Aufruf wie neulich rein:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.btnTestClick(Sender: TObject);
begin
  AnzahlLebenderNachbarn(00); // Test-Aufruf
end;

Und, was passiert?

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 15.03.15 20:30 
ok, das war keine gute idee, die vorher- und nacherwerte sind identisch :/

ich kann genau das andere rausnehmen,also für x >(N-1), das klapppt dann wieder wie es soll
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 15.03.15 22:26 
Moin!

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
ok, das war keine gute idee, die vorher- und nacherwerte sind identisch :/
;)

user profile iconNini hat folgendes geschrieben Zum zitierten Posting springen:
ich kann genau das andere rausnehmen,also für x >(N-1), das klapppt dann wieder wie es soll
Genau so ist es, du hast ja für N <= x < 2N genau den Effekt, den mod N bewirkt, wenn du x -N rechnest. :idea: Deshalb ist es die zweite if-Gruppe, die rausfällt.

Wie werden wir denn nun die erste if-Gruppe auch noch los? :gruebel: Hier wirkt ja leider mod N nicht, weil wir bereits festgestellt haben, dass -1 mod N auch = -1 bleibt (das war das mit dem "doof" :zwinker:)...

Ideen? :D

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: So 15.03.15 22:30 
nein, also um ehrlich zu sein, fällt mir nichts ein, wie die If-anweisung weg könnte :(
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 16.03.15 00:05 
Moin!

OK, dann fassen wir nochmal zusammen:

a) wir können "mod N" nutzen, um Werte größer N "abzufangen"
b) man kann beliebig oft N addieren, solange man irgendwann "mod N" rechnet, ist das egal (also zumindest in unserem Rahmen hier)

Das waren die beiden Erkenntnisse, die wir aus dem Mathe-Vortrag vorhin gewonnen haben (oder haben sollten :angel:). a) haben wird schon "untergebracht", was bleibt noch? :zwinker:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: Mo 16.03.15 17:26 
bei mir hängt es anscheinend, ich sehe einfach keine möglichkeit das noch weoiter zu vereinfachen ;/
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 16.03.15 17:29 
Moin!

Wir haben in der ersten if-Gruppe N addiert, wenn x < 0 ist. Könnte man nicht "gefahrlos" immer x addieren? (weil wir doch mod N nutzen) :idea:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: Mo 16.03.15 17:35 
aber wenn x = -1 dann ist doch wenn ich x addiere das neue x=-2 oder hab ich nen denkfehler?
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 16.03.15 17:37 
Moin!

x addieren... :autsch: :oops: :lol:

N addieren natürlich! :P

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: Mo 16.03.15 17:44 
also N addieren geht problemlos
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 16.03.15 17:47 
Moin!

Ja dann, her mit dem Codevorschlag. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Nini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 170
Erhaltene Danke: 12



BeitragVerfasst: Mo 16.03.15 17:52 
also ich würd es ja einfahc so lassen, weiß nicht, was ich so genau ändern soll

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
  
function TForm1.LebtNachbar(x, y: Integer): Boolean;
begin
  if (x < 0then
    x := x +N;
  if (y < 0then
    y := y +N;
  
  Result := Feld[x mod N, y mod N];
end;