home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / C80TCOG.ZIP / MULT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-08  |  2.4 KB  |  90 lines

  1. /* isqrt - returns greatest unsigned int <= sqrt(arg)           */
  2. /*                                                      1982/10/16 22:43
  3.         submitted by    William G. Hutchison, Jr.
  4.                         P.O. Box 278
  5.                         Exton, PA 19481-0278
  6.                         U.S.A.
  7.  
  8.                         CompuServe 70665,1307
  9.  
  10.  */
  11.  
  12. #ifdef UNIX
  13. unsigned
  14. #endif
  15. isqrt(a)unsigned a;
  16. {
  17.         register unsigned x, y, z;
  18.         for(x= 0, y=z=1; y<=a; x++) y+=(z+=2);
  19.         return(x);
  20. }
  21. /* end isqrt */
  22.  
  23. /* Alagic, Suad & Arbib, Michael A. "The Design of Well Structured
  24. and Correct Programs" Springer-Verlag, New York 1978, p. 166.   */
  25.  
  26. #ifdef MAINLY
  27. #else
  28. #include "c80def.h"
  29. #include "printf.c"
  30.  
  31. main(){
  32.         unsigned n;
  33.         for (n= 0; n < 32767; n++)
  34.                 printf("%3d <= sqrt(%d)\n", isqrt(n), n);
  35. } /* end main */
  36.  
  37. #endif
  38. /* mult - multiply unsigned ints by the Rhind papyrus method    */
  39. /*                                                      1982/10/16 22:49
  40.         submitted by    William G. Hutchison, Jr.
  41.                         P.O. Box 278
  42.                         Exton, PA 19341-0278
  43.                         U.S.A.
  44.  
  45.                         CompuServe 70665,1307
  46.  
  47.  */
  48.  
  49. #ifdef UNIX
  50. unsigned
  51. #endif
  52. mult(a,b)unsigned a, b;
  53. {
  54.         register unsigned x, y, z; 
  55.         x= a; 
  56.         y= b; 
  57.         z= 0; 
  58.         while(y){
  59.                 while(y && ((y&1) == 0)){
  60.                         y>>=1; 
  61.                         x+=x;
  62.                 }
  63.                 y--;
  64.                 z+=x;
  65.         }
  66.         return(z);
  67. } /* end mult */
  68.  
  69. /* Alagic, Suad & Arbib, Michael A. "The Design of Well Structured
  70. and Correct Programs" Springer-Verlag, New York 1978, p. 243.   */
  71.  
  72. #ifdef MAINLY
  73. #else
  74. #include "c80def.h"
  75. #include "printf.c"
  76.  
  77. main(){
  78.         unsigned a, b, e;
  79.         printf("Starting test of mult\n");
  80.         for(e= a= 0; a < 256; a++){
  81.                 for(b= 0; b < 256; b++)
  82.                         if(a*b != mult(a,b))
  83.                                 printf("Error #%d: %d*%d = %d?\n", ++e,
  84.                 a, b, mult(a,b));
  85.                 printf("%d tests finished.\n", 256*(a+1));
  86.         }
  87.         printf("Testing found %d errors.\n", e);
  88. } /* end main */
  89. #endif
  90.