Home | History | Annotate | Line # | Download | only in factor
factor.c revision 1.1
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1989 The Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * This code is derived from software contributed to Berkeley by
      6  1.1  cgd  * Landon Curt Noll.
      7  1.1  cgd  *
      8  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1  cgd  * modification, are permitted provided that the following conditions
     10  1.1  cgd  * are met:
     11  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1  cgd  *    must display the following acknowledgement:
     18  1.1  cgd  *	This product includes software developed by the University of
     19  1.1  cgd  *	California, Berkeley and its contributors.
     20  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  cgd  *    may be used to endorse or promote products derived from this software
     22  1.1  cgd  *    without specific prior written permission.
     23  1.1  cgd  *
     24  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  cgd  * SUCH DAMAGE.
     35  1.1  cgd  */
     36  1.1  cgd 
     37  1.1  cgd #ifndef lint
     38  1.1  cgd char copyright[] =
     39  1.1  cgd "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
     40  1.1  cgd  All rights reserved.\n";
     41  1.1  cgd #endif /* not lint */
     42  1.1  cgd 
     43  1.1  cgd #ifndef lint
     44  1.1  cgd static char sccsid[] = "@(#)factor.c	4.4 (Berkeley) 6/1/90";
     45  1.1  cgd #endif /* not lint */
     46  1.1  cgd 
     47  1.1  cgd /*
     48  1.1  cgd  * factor - factor a number into primes
     49  1.1  cgd  *
     50  1.1  cgd  * By: Landon Curt Noll   chongo (at) toad.com,   ...!{sun,tolsoft}!hoptoad!chongo
     51  1.1  cgd  *
     52  1.1  cgd  *   chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
     53  1.1  cgd  *
     54  1.1  cgd  * usage:
     55  1.1  cgd  *	factor [number] ...
     56  1.1  cgd  *
     57  1.1  cgd  * The form of the output is:
     58  1.1  cgd  *
     59  1.1  cgd  *	number: factor1 factor1 factor2 factor3 factor3 factor3 ...
     60  1.1  cgd  *
     61  1.1  cgd  * where factor1 < factor2 < factor3 < ...
     62  1.1  cgd  *
     63  1.1  cgd  * If no args are given, the list of numbers are read from stdin.
     64  1.1  cgd  */
     65  1.1  cgd 
     66  1.1  cgd #include <stdio.h>
     67  1.1  cgd #include <ctype.h>
     68  1.1  cgd #include "primes.h"
     69  1.1  cgd 
     70  1.1  cgd /*
     71  1.1  cgd  * prime[i] is the (i-1)th prime.
     72  1.1  cgd  *
     73  1.1  cgd  * We are able to sieve 2^32-1 because this byte table yields all primes
     74  1.1  cgd  * up to 65537 and 65537^2 > 2^32-1.
     75  1.1  cgd  */
     76  1.1  cgd extern ubig prime[];
     77  1.1  cgd extern ubig *pr_limit;	/* largest prime in the prime array */
     78  1.1  cgd 
     79  1.1  cgd #define MAX_LINE 255	/* max line allowed on stdin */
     80  1.1  cgd 
     81  1.1  cgd void pr_fact();		/* print factors of a value */
     82  1.1  cgd long small_fact();	/* find smallest factor of a value */
     83  1.1  cgd char *read_num_buf();	/* read a number buffer */
     84  1.1  cgd char *program;		/* name of this program */
     85  1.1  cgd 
     86  1.1  cgd main(argc, argv)
     87  1.1  cgd 	int argc;	/* arg count */
     88  1.1  cgd 	char *argv[];	/* the args */
     89  1.1  cgd {
     90  1.1  cgd 	int arg;	/* which arg to factor */
     91  1.1  cgd 	long val;	/* the value to factor */
     92  1.1  cgd 	char buf[MAX_LINE+1];	/* input buffer */
     93  1.1  cgd 
     94  1.1  cgd 	/* parse args */
     95  1.1  cgd 	program = argv[0];
     96  1.1  cgd 	if (argc >= 2) {
     97  1.1  cgd 
     98  1.1  cgd 		/* factor each arg */
     99  1.1  cgd 		for (arg=1; arg < argc; ++arg) {
    100  1.1  cgd 
    101  1.1  cgd 			/* process the buffer */
    102  1.1  cgd 			if (read_num_buf(NULL, argv[arg]) == NULL) {
    103  1.1  cgd 				fprintf(stderr, "%s: ouch\n", program);
    104  1.1  cgd 				exit(1);
    105  1.1  cgd 			}
    106  1.1  cgd 
    107  1.1  cgd 			/* factor the argument */
    108  1.1  cgd 			if (sscanf(argv[arg], "%ld", &val) == 1) {
    109  1.1  cgd 				pr_fact(val);
    110  1.1  cgd 			} else {
    111  1.1  cgd 				fprintf(stderr, "%s: ouch\n", program);
    112  1.1  cgd 				exit(1);
    113  1.1  cgd 			}
    114  1.1  cgd 		}
    115  1.1  cgd 
    116  1.1  cgd 	/* no args supplied, read numbers from stdin */
    117  1.1  cgd 	} else {
    118  1.1  cgd 		/*
    119  1.1  cgd 		 * read asciii numbers from input
    120  1.1  cgd 		 */
    121  1.1  cgd 		while (read_num_buf(stdin, buf) != NULL) {
    122  1.1  cgd 
    123  1.1  cgd 			/* factor the argument */
    124  1.1  cgd 			if (sscanf(buf, "%ld", &val) == 1) {
    125  1.1  cgd 				pr_fact(val);
    126  1.1  cgd 			}
    127  1.1  cgd 		}
    128  1.1  cgd 	}
    129  1.1  cgd 	exit(0);
    130  1.1  cgd }
    131  1.1  cgd 
    132  1.1  cgd /*
    133  1.1  cgd  * read_num_buf - read a number buffer from a stream
    134  1.1  cgd  *
    135  1.1  cgd  * Read a number on a line of the form:
    136  1.1  cgd  *
    137  1.1  cgd  *	^[ \t]*\([+-]?[0-9][0-9]\)*.*$
    138  1.1  cgd  *
    139  1.1  cgd  * where ? is a 1-or-0 operator and the number is within \( \).
    140  1.1  cgd  *
    141  1.1  cgd  * If does not match the above pattern, it is ignored and a new
    142  1.1  cgd  * line is read.  If the number is too large or small, we will
    143  1.1  cgd  * print ouch and read a new line.
    144  1.1  cgd  *
    145  1.1  cgd  * We have to be very careful on how we check the magnitude of the
    146  1.1  cgd  * input.  We can not use numeric checks because of the need to
    147  1.1  cgd  * check values against maximum numeric values.
    148  1.1  cgd  *
    149  1.1  cgd  * This routine will return a line containing a ascii number between
    150  1.1  cgd  * NEG_SEMIBIG and SEMIBIG, or it will return NULL.
    151  1.1  cgd  *
    152  1.1  cgd  * If the stream is NULL then buf will be processed as if were
    153  1.1  cgd  * a single line stream.
    154  1.1  cgd  *
    155  1.1  cgd  * returns:
    156  1.1  cgd  *	char *	pointer to leading digit, + or -
    157  1.1  cgd  *	NULL	EOF or error
    158  1.1  cgd  */
    159  1.1  cgd char *
    160  1.1  cgd read_num_buf(input, buf)
    161  1.1  cgd 	FILE *input;		/* input stream or NULL */
    162  1.1  cgd 	char *buf;		/* input buffer */
    163  1.1  cgd {
    164  1.1  cgd 	static char limit[MAX_LINE+1];	/* ascii value of SEMIBIG */
    165  1.1  cgd 	static int limit_len;		/* digit count of limit */
    166  1.1  cgd 	static char neg_limit[MAX_LINE+1];	/* value of NEG_SEMIBIG */
    167  1.1  cgd 	static int neg_limit_len;		/* digit count of neg_limit */
    168  1.1  cgd 	int len;			/* digits in input (excluding +/-) */
    169  1.1  cgd 	char *s;	/* line start marker */
    170  1.1  cgd 	char *d;	/* first digit, skip +/- */
    171  1.1  cgd 	char *p;	/* scan pointer */
    172  1.1  cgd 	char *z;	/* zero scan pointer */
    173  1.1  cgd 
    174  1.1  cgd 	/* form the ascii value of SEMIBIG if needed */
    175  1.1  cgd 	if (!isascii(limit[0]) || !isdigit(limit[0])) {
    176  1.1  cgd 		sprintf(limit, "%ld", SEMIBIG);
    177  1.1  cgd 		limit_len = strlen(limit);
    178  1.1  cgd 		sprintf(neg_limit, "%ld", NEG_SEMIBIG);
    179  1.1  cgd 		neg_limit_len = strlen(neg_limit)-1;	/* exclude - */
    180  1.1  cgd 	}
    181  1.1  cgd 
    182  1.1  cgd 	/*
    183  1.1  cgd 	 * the search for a good line
    184  1.1  cgd 	 */
    185  1.1  cgd 	if (input != NULL && fgets(buf, MAX_LINE, input) == NULL) {
    186  1.1  cgd 		/* error or EOF */
    187  1.1  cgd 		return NULL;
    188  1.1  cgd 	}
    189  1.1  cgd 	do {
    190  1.1  cgd 
    191  1.1  cgd 		/* ignore leading whitespace */
    192  1.1  cgd 		for (s=buf; *s && s < buf+MAX_LINE; ++s) {
    193  1.1  cgd 			if (!isascii(*s) || !isspace(*s)) {
    194  1.1  cgd 				break;
    195  1.1  cgd 			}
    196  1.1  cgd 		}
    197  1.1  cgd 
    198  1.1  cgd 		/* skip over any leading + or - */
    199  1.1  cgd 		if (*s == '+' || *s == '-') {
    200  1.1  cgd 			d = s+1;
    201  1.1  cgd 		} else {
    202  1.1  cgd 			d = s;
    203  1.1  cgd 		}
    204  1.1  cgd 
    205  1.1  cgd 		/* note leading zeros */
    206  1.1  cgd 		for (z=d; *z && z < buf+MAX_LINE; ++z) {
    207  1.1  cgd 			if (*z != '0') {
    208  1.1  cgd 				break;
    209  1.1  cgd 			}
    210  1.1  cgd 		}
    211  1.1  cgd 
    212  1.1  cgd 		/* scan for the first non-digit */
    213  1.1  cgd 		for (p=d; *p && p < buf+MAX_LINE; ++p) {
    214  1.1  cgd 			if (!isascii(*p) || !isdigit(*p)) {
    215  1.1  cgd 				break;
    216  1.1  cgd 			}
    217  1.1  cgd 		}
    218  1.1  cgd 
    219  1.1  cgd 		/* ignore empty lines */
    220  1.1  cgd 		if (p == d) {
    221  1.1  cgd 			continue;
    222  1.1  cgd 		}
    223  1.1  cgd 		*p = '\0';
    224  1.1  cgd 
    225  1.1  cgd 		/* object if too many digits */
    226  1.1  cgd 		len = strlen(z);
    227  1.1  cgd 		len = (len<=0) ? 1 : len;
    228  1.1  cgd 		if (*s == '-') {
    229  1.1  cgd 			/* accept if digit count is below limit */
    230  1.1  cgd 			if (len < neg_limit_len) {
    231  1.1  cgd 				/* we have good input */
    232  1.1  cgd 				return s;
    233  1.1  cgd 
    234  1.1  cgd 			/* reject very large numbers */
    235  1.1  cgd 			} else if (len > neg_limit_len) {
    236  1.1  cgd 				fprintf(stderr, "%s: ouch\n", program);
    237  1.1  cgd 				exit(1);
    238  1.1  cgd 
    239  1.1  cgd 			/* carefully check against near limit numbers */
    240  1.1  cgd 			} else if (strcmp(z, neg_limit+1) > 0) {
    241  1.1  cgd 				fprintf(stderr, "%s: ouch\n", program);
    242  1.1  cgd 				exit(1);
    243  1.1  cgd 			}
    244  1.1  cgd 			/* number is near limit, but is under it */
    245  1.1  cgd 			return s;
    246  1.1  cgd 
    247  1.1  cgd 		} else {
    248  1.1  cgd 			/* accept if digit count is below limit */
    249  1.1  cgd 			if (len < limit_len) {
    250  1.1  cgd 				/* we have good input */
    251  1.1  cgd 				return s;
    252  1.1  cgd 
    253  1.1  cgd 			/* reject very large numbers */
    254  1.1  cgd 			} else if (len > limit_len) {
    255  1.1  cgd 				fprintf(stderr, "%s: ouch\n", program);
    256  1.1  cgd 				exit(1);
    257  1.1  cgd 
    258  1.1  cgd 			/* carefully check against near limit numbers */
    259  1.1  cgd 			} else if (strcmp(z, limit) > 0) {
    260  1.1  cgd 				fprintf(stderr, "%s: ouch\n", program);
    261  1.1  cgd 				exit(1);
    262  1.1  cgd 			}
    263  1.1  cgd 			/* number is near limit, but is under it */
    264  1.1  cgd 			return s;
    265  1.1  cgd 		}
    266  1.1  cgd 	} while (input != NULL && fgets(buf, MAX_LINE, input) != NULL);
    267  1.1  cgd 
    268  1.1  cgd 	/* error or EOF */
    269  1.1  cgd 	return NULL;
    270  1.1  cgd }
    271  1.1  cgd 
    272  1.1  cgd 
    273  1.1  cgd /*
    274  1.1  cgd  * pr_fact - print the factors of a number
    275  1.1  cgd  *
    276  1.1  cgd  * If the number is 0 or 1, then print the number and return.
    277  1.1  cgd  * If the number is < 0, print -1, negate the number and continue
    278  1.1  cgd  * processing.
    279  1.1  cgd  *
    280  1.1  cgd  * Print the factors of the number, from the lowest to the highest.
    281  1.1  cgd  * A factor will be printed numtiple times if it divides the value
    282  1.1  cgd  * multiple times.
    283  1.1  cgd  *
    284  1.1  cgd  * Factors are printed with leading tabs.
    285  1.1  cgd  */
    286  1.1  cgd void
    287  1.1  cgd pr_fact(val)
    288  1.1  cgd 	long val;	/* factor this value */
    289  1.1  cgd {
    290  1.1  cgd 	ubig *fact;	/* the factor found */
    291  1.1  cgd 
    292  1.1  cgd 	/* firewall - catch 0 and 1 */
    293  1.1  cgd 	switch (val) {
    294  1.1  cgd 	case -2147483648:
    295  1.1  cgd 		/* avoid negation problems */
    296  1.1  cgd 		puts("-2147483648: -1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2\n");
    297  1.1  cgd 		return;
    298  1.1  cgd 	case -1:
    299  1.1  cgd 		puts("-1: -1\n");
    300  1.1  cgd 		return;
    301  1.1  cgd 	case 0:
    302  1.1  cgd 		exit(0);
    303  1.1  cgd 	case 1:
    304  1.1  cgd 		puts("1: 1\n");
    305  1.1  cgd 		return;
    306  1.1  cgd 	default:
    307  1.1  cgd 		if (val < 0) {
    308  1.1  cgd 			val = -val;
    309  1.1  cgd 			printf("%ld: -1", val);
    310  1.1  cgd 		} else {
    311  1.1  cgd 			printf("%ld:", val);
    312  1.1  cgd 		}
    313  1.1  cgd 		fflush(stdout);
    314  1.1  cgd 		break;
    315  1.1  cgd 	}
    316  1.1  cgd 
    317  1.1  cgd 	/*
    318  1.1  cgd 	 * factor value
    319  1.1  cgd 	 */
    320  1.1  cgd 	fact = &prime[0];
    321  1.1  cgd 	while (val > 1) {
    322  1.1  cgd 
    323  1.1  cgd 		/* look for the smallest factor */
    324  1.1  cgd 		do {
    325  1.1  cgd 			if (val%(long)*fact == 0) {
    326  1.1  cgd 				break;
    327  1.1  cgd 			}
    328  1.1  cgd 		} while (++fact <= pr_limit);
    329  1.1  cgd 
    330  1.1  cgd 		/* watch for primes larger than the table */
    331  1.1  cgd 		if (fact > pr_limit) {
    332  1.1  cgd 			printf(" %ld\n", val);
    333  1.1  cgd 			return;
    334  1.1  cgd 		}
    335  1.1  cgd 
    336  1.1  cgd 		/* divide factor out until none are left */
    337  1.1  cgd 		do {
    338  1.1  cgd 			printf(" %ld", *fact);
    339  1.1  cgd 			val /= (long)*fact;
    340  1.1  cgd 		} while ((val % (long)*fact) == 0);
    341  1.1  cgd 		fflush(stdout);
    342  1.1  cgd 		++fact;
    343  1.1  cgd 	}
    344  1.1  cgd 	putchar('\n');
    345  1.1  cgd 	return;
    346  1.1  cgd }
    347