Advertisement
hpolzer

Russian peasant multiplication

Mar 21st, 2016
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.65 KB | None | 0 0
  1. {
  2.     rbm.pas multiplies two numbers given at the command line using the "Russian
  3.     peasant multiplication. To compile run "fpc rbm.pas".
  4.     Copyright (C) <September 22, 2013> Henning Polzer,
  5.     send comments and error reports to: h underscore polzer at gmx dot de.
  6.  
  7.     This program is free software; you can redistribute it and/or
  8.     modify it under the terms of the GNU General Public License
  9.     as published by the Free Software Foundation; either version 2
  10.     of the License, or (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  20. }
  21.  
  22. PROGRAM russische_bauernmultiplikation;
  23. CONST br = 7;                   { zur formatierten Ausgabe }
  24. TYPE ganzzahl = longint;
  25. VAR zahl1, zahl2: ganzzahl;
  26.     fehlercode: integer;        { muss integer sein }
  27.  
  28.  
  29. PROCEDURE rbm (a, b: ganzzahl);
  30. VAR halb, links, doppel, rechts, summe: ganzzahl;
  31.     flag: boolean;
  32.  
  33. BEGIN { rbm }
  34.   links := a;
  35.   rechts := b;
  36.   summe := 0;
  37.   flag := false;
  38.  
  39.   IF b < a THEN                 { kleineren Faktor nach links }
  40.   BEGIN
  41.     links := b;
  42.     rechts := a;
  43.     flag := true
  44.   END; { if }
  45.  
  46.   write (links:br, ' ', rechts:br);
  47.   IF odd (links) THEN summe := summe + rechts   { Startwert ggf. einbeziehen }
  48.     ELSE write ('<gestrichen');
  49.  
  50.   write (' Startwerte');
  51.   IF flag = true THEN write (' (Positionen getauscht)');
  52.   writeln;
  53.  
  54.   REPEAT
  55.     halb := links DIV 2;
  56.     doppel := rechts * 2;
  57.     write (halb:br, ' ');
  58.     IF NOT odd (halb) THEN write (doppel:br, '<gestrichen') { <-- links gerade }
  59.     ELSE BEGIN                              { links ungerade: }
  60.       write (doppel:br);
  61.       summe := summe + doppel
  62.     END; { else }
  63.     writeln;
  64.     links := halb;
  65.     rechts := doppel    
  66.   UNTIL links < 2;
  67.  
  68.   writeln;
  69.   IF flag = false THEN write (a, ' * ', b)
  70.     ELSE write (b, ' * ', a);
  71.   write (' = ', summe, ', stimmt mit konventioneller Berechnung');
  72.   IF a * b <> summe THEN write (' nicht');
  73.   writeln (' ueberein.')
  74. END; { rbm }
  75.  
  76.  
  77. BEGIN { Hauptprogramm }
  78.   val (paramstr (1), zahl1, fehlercode); { Kommandozeilenparameter einlesen }
  79.   val (paramstr (2), zahl2, fehlercode);
  80.  
  81.   IF (paramcount = 2) AND       { genau zwei Zahlen > 0 eingeben }
  82.      (fehlercode = 0) AND
  83.      (zahl1 > 0) AND
  84.      (zahl2 > 0) THEN rbm (zahl1, zahl2)
  85.     ELSE writeln ('Aufruf: rbm zahl1 zahl2')
  86. END.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement