Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- middle_square_method.c 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 "gcc -ansi -o msm middle_square_method.c".
- 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.
- */
- #include <math.h> /* fabs(), floor(), log10(), pow() */
- #include <stdio.h> /* printf(), scanf() */
- #include <stdlib.h> /* atol() */
- #define OBERGRENZE 50 /* für sicheren Programmabruch, willkuerlich gesetzt */
- typedef unsigned long ganzzahl;
- ganzzahl stellen (wert) /* Stellenzahl ermitteln */
- {
- if (wert != 0) return floor (log10(fabs(wert)))+1; else return 1;
- }
- ganzzahl qmg (z)
- {
- ganzzahl quadrat=z*z, diff=stellen(quadrat)-stellen(z), links=floor(diff/2), t;
- if (z < 4) return 0;
- quadrat=floor(quadrat/powl(10,diff-links));
- t=pow(10,stellen(quadrat)-links);
- return quadrat%t;
- }
- int main (int argc, char *argv[])
- {
- ganzzahl erg, i=1, z, verlassen=0;
- if (((argc==2) && (z = fabs(atol(argv[1])))) != 0) {
- do {
- erg = qmg (z);
- if (erg == z) verlassen=1;
- z = erg;
- printf ("%ld ", erg);
- i += 1;
- } while ((verlassen==0) && (erg != 0) && (i < OBERGRENZE));
- printf ("\n");
- } else printf ("Aufruf: qmg eine_zahl_groesser_null\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement