Annotation of coherent/a/usr/src/sample/factor.c, revision 1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.