home *** CD-ROM | disk | FTP | other *** search
/ Vectronix 2 / VECTRONIX2.iso / FILES_07 / MARK_WC1.LZH / SRC / EXAMPLE3.C < prev    next >
C/C++ Source or Header  |  1988-04-27  |  2KB  |  124 lines

  1. /*
  2.  * Factor prints out the prime factorization of numbers.  If there are
  3.  * any arguments, then it factors these.  If there are no arguments,
  4.  * then it reads stdin until either EOF or the number zero or a non-numeric
  5.  * non-white-space character.  Since factor does all of its calculations
  6.  * in double format, the largest number which can be handled is quite
  7.  * large.
  8.  */
  9. #include <stdio.h>
  10. #include <math.h>
  11. #include <ctype.h>
  12.  
  13.  
  14. #define    NUL    '\0'
  15. #define    ERROR    0x10            /* largest input base */
  16. #define    MAXNUM    200            /* max number of chars in number */
  17.  
  18.  
  19. main(argc, argv)
  20. int        argc;
  21. register char    *argv[];
  22. {
  23.     register char    *chp;
  24.     double        n;
  25.     double        atod();
  26.     char        *getnum();
  27.  
  28.     if (argc != 1)
  29.         while ((chp=*++argv) != NULL && (n=atod(chp)) != 0)
  30.             factor(n);
  31.     else
  32.         while ((chp=getnum()) != NULL && (n=atod(chp)) != 0)
  33.             factor(n);
  34.     return (0);
  35. }
  36.  
  37.  
  38. die(str)
  39. char    *str;
  40. {
  41.     fprintf(stderr, "%r\n", &str);
  42.     exit(1);
  43. }
  44.  
  45. usage()
  46. {
  47.     die("usage: factor [number number ...]");
  48. }
  49.  
  50.  
  51. char    *
  52. getnum()
  53. {
  54.     register char    *chp,
  55.             ch;
  56.     static char    res[MAXNUM+1];
  57.  
  58.     do {
  59.         ch = getchar();
  60.     } while (isascii(ch) && isspace(ch));
  61.     if (!isascii(ch) || todigit(ch) == ERROR)
  62.         return (NULL);
  63.     for (chp=res; isascii(ch) && !isspace(ch); ch=getchar())
  64.         if (chp < &res[MAXNUM])
  65.             *chp++ = ch;
  66.     if (chp >= &res[MAXNUM])
  67.         die("Number too big");
  68.     *chp++ = NUL;
  69.     return (res);
  70. }
  71.  
  72.  
  73.  
  74.  
  75. /*
  76.  * Todigit converts the char `ch' to an integer equivalent, assuming
  77.  * that `ch' is a digit or `a'-`f' or `A'-`F'.  If this is not true,
  78.  * then it returns ERROR.
  79.  */
  80. todigit(ch)
  81. register int    ch;
  82. {
  83.     if (!isascii(ch))
  84.         return (ERROR);
  85.     if (isdigit(ch))
  86.         return (ch - '0' + 0);
  87.     if (isupper(ch))
  88.         ch = tolower(ch);
  89.     if ('a' <= ch && ch <= 'f')
  90.         return (ch - 'a' + 0xa);
  91.     return (ERROR);
  92. }
  93.  
  94.  
  95. /*
  96.  * Factor is the routine that actually factors the double `n'.
  97.  * It writes the prime factors to standard output.
  98.  */
  99. factor(n)
  100. double    n;
  101. {
  102.     double        temp,
  103.             limit,
  104.             try;
  105.  
  106.     while (n > 1 && modf(n/2, &temp) == 0) {
  107.         printf("2 ");
  108.         n = temp;
  109.     }
  110.     limit = sqrt(n);
  111.     for (try=3; try <= limit; try += 2) {
  112.         if (modf(n/try, &temp) != 0)
  113.             continue;
  114.         do {
  115.             printf("%.0f ", try);
  116.             n = temp;
  117.         } while (modf(n/try, &temp) == 0);
  118.         limit = sqrt(n);
  119.     }
  120.     if (n > 1)
  121.         printf("%.0f", n);
  122.     putchar('\n');
  123. }
  124.