home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource4 / 247_01 / hail.c < prev    next >
Text File  |  1989-04-19  |  896b  |  38 lines

  1. /*
  2.  *   Program to investigate hailstone numbers.
  3.  *   Gruenberger F. 'Computer Recreations' Scientific American. April 1984.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "miracl.h"
  8.  
  9. main ()
  10. {  /*  hailstone numbers  */
  11.     int iter,r;
  12.     big x,y,mx;
  13.     mirsys(100,10);
  14.     x=mirvar(0);
  15.     y=mirvar(0);
  16.     mx=mirvar(0);
  17.     iter=0;
  18.     printf("number = \n");
  19.     innum(x,stdin);
  20.     do
  21.     { /* main loop */
  22.         if (compare(x,mx)>0) copy(x,mx);
  23.         r=subdiv(x,2,y);
  24.         if (r!=0)
  25.         { /* what goes up ... */
  26.             premult(x,3,x);
  27.             incr(x,1,x);
  28.         }
  29.         /* ... must come down */
  30.         else copy(y,x);
  31.         otnum(x,stdout);
  32.         iter++;
  33.     } while (size(x)!=1);
  34.     printf("path length = %d \n",iter);
  35.     printf("maximum = ");
  36.     otnum(mx,stdout);
  37. }
  38.