Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- middle_square_method.pas computes pseudorandom numbers starting with a number
- given at the command line. (Outdated method, for educational purposes only.
- Try 9319 or 12314.) To compile run "fpc middle_square_method.pas".
- Copyright (C) <February 29, 2016> Henning Polzer,
- send comments and error reports to: h underscore polzer at gmx dot de.
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- }
- PROGRAM quadratmittengenerator;
- CONST obergrenze = 50; { als sichere Abbruchbedingung }
- TYPE Tgz = cardinal; { cardinal von 0 bis 2^32-1, Standard: integer }
- VAR fehlercode: integer; { muss als integer vereinbart werden }
- erg, i, z: Tgz;
- verlassen: boolean;
- FUNCTION qmg (zahl: Tgz): Tgz;
- VAR diff, links, quadrat: Tgz;
- FUNCTION zp (exp: Tgz): Tgz; { Zehnerpotenz berechnen }
- VAR h, i: Tgz;
- BEGIN
- h := 1;
- FOR i := 1 TO exp DO
- h := h * 10;
- zp := h
- END; { zp }
- FUNCTION stellen (wert: Tgz): Tgz; { Stellenzahl ermitteln }
- BEGIN
- IF z <> 0 THEN stellen := trunc ((abs (ln (wert) / ln (10)))) + 1 ELSE stellen := 1
- END; { stellen }
- BEGIN { qmg }
- quadrat := zahl * zahl;
- diff := stellen (quadrat) - stellen (zahl);
- links := trunc (diff / 2);
- quadrat := trunc (quadrat / zp (diff - links));
- IF zahl < 4 THEN qmg := 0 ELSE
- qmg := quadrat MOD zp (stellen (quadrat) - links)
- END; { qmg }
- BEGIN { Hauptprogramm }
- val (paramstr (1), z, fehlercode); { Standard: read (z) }
- IF (paramcount = 1) AND (fehlercode = 0) AND (z > 0) THEN { genau eine Zahl > 0 eingeben }
- BEGIN
- i := 1;
- verlassen := false;
- REPEAT
- erg := qmg (z);
- verlassen := erg = z;
- z := erg;
- write (erg, ' ');
- i := i + 1 { Obergrenze sichere Abbruchbedingung }
- UNTIL verlassen OR (erg = 0) OR (i > obergrenze); { Groesse willkuerlich gesetzt }
- writeln
- END ELSE writeln ('Aufruf: qmg eine_zahl_groesser_null')
- END.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement