1 /* $OpenBSD: factor.c,v 1.19 2009/10/27 23:59:24 deraadt Exp $ */ 2 /* $NetBSD: factor.c,v 1.5 1995/03/23 08:28:07 cgd Exp $ */ 3 4 /* 5 * Copyright (c) 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Landon Curt Noll. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 /* 37 * factor - factor a number into primes 38 * 39 * By: Landon Curt Noll chongo@toad.com, ...!{sun,tolsoft}!hoptoad!chongo 40 * 41 * chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\ 42 * 43 * usage: 44 * factor [number ...] 45 * 46 * The form of the output is: 47 * 48 * number: factor1 factor1 factor2 factor3 factor3 factor3 ... 49 * 50 * where factor1 < factor2 < factor3 < ... 51 * 52 * If no args are given, the list of numbers are read from stdin. 53 */ 54 55 #include <sys/types.h> 56 #include <err.h> 57 #include <ctype.h> 58 #include <errno.h> 59 #include <limits.h> 60 #include <math.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <unistd.h> 65 66 #include "primes.h" 67 68 /* 69 * prime[i] is the (i+1)th prime. 70 * 71 * We are able to sieve 2^32-1 because this byte table yields all primes 72 * up to 65537 and 65537^2 > 2^32-1. 73 */ 74 extern const ubig prime[]; 75 extern const ubig *pr_limit; /* largest prime in the prime array */ 76 extern const char pattern[]; 77 extern const int pattern_size; 78 79 void pr_fact(u_int64_t); /* print factors of a value */ 80 void pr_bigfact(u_int64_t); 81 void usage(void); 82 83 int 84 main(int argc, char *argv[]) 85 { 86 u_int64_t val; 87 int ch; 88 char *p, buf[100]; /* > max number of digits. */ 89 90 while ((ch = getopt(argc, argv, "")) != -1) 91 switch (ch) { 92 case '?': 93 default: 94 usage(); 95 } 96 argc -= optind; 97 argv += optind; 98 99 /* No args supplied, read numbers from stdin. */ 100 if (argc == 0) 101 for (;;) { 102 if (fgets(buf, sizeof(buf), stdin) == NULL) { 103 if (ferror(stdin)) 104 err(1, "stdin"); 105 exit (0); 106 } 107 buf[strcspn(buf, "\n")] = '\0'; 108 for (p = buf; isblank(*p); ++p); 109 if (*p == '\0') 110 continue; 111 if (*p == '-') 112 errx(1, "negative numbers aren't permitted."); 113 errno = 0; 114 val = strtouq(buf, &p, 10); 115 if (errno) 116 err(1, "%s", buf); 117 for (; isblank(*p); ++p); 118 if (*p != '\0') 119 errx(1, "%s: illegal numeric format.", buf); 120 pr_fact(val); 121 } 122 /* Factor the arguments. */ 123 else 124 for (; *argv != NULL; ++argv) { 125 if (argv[0][0] == '-') 126 errx(1, "negative numbers aren't permitted."); 127 errno = 0; 128 val = strtouq(argv[0], &p, 10); 129 if (errno) 130 err(1, "%s", argv[0]); 131 if (*p != '\0') 132 errx(1, "%s: illegal numeric format.", argv[0]); 133 pr_fact(val); 134 } 135 exit(0); 136 } 137 138 /* 139 * pr_fact - print the factors of a number 140 * 141 * If the number is 0 or 1, then print the number and return. 142 * If the number is < 0, print -1, negate the number and continue 143 * processing. 144 * 145 * Print the factors of the number, from the lowest to the highest. 146 * A factor will be printed multiple times if it divides the value 147 * multiple times. 148 * 149 * Factors are printed with leading tabs. 150 */ 151 void 152 pr_fact(u_int64_t val) /* Factor this value. */ 153 { 154 const ubig *fact; /* The factor found. */ 155 156 /* Firewall - catch 0 and 1. */ 157 if (val == 0) /* Historical practice; 0 just exits. */ 158 exit(0); 159 if (val == 1) { 160 (void)printf("1: 1\n"); 161 return; 162 } 163 164 /* Factor value. */ 165 (void)printf("%llu:", val); 166 fflush(stdout); 167 for (fact = &prime[0]; val > 1; ++fact) { 168 /* Look for the smallest factor. */ 169 do { 170 if (val % (long)*fact == 0) 171 break; 172 } while (++fact <= pr_limit); 173 174 /* Watch for primes larger than the table. */ 175 if (fact > pr_limit) { 176 if (val > BIG) 177 pr_bigfact(val); 178 else 179 (void)printf(" %llu", val); 180 break; 181 } 182 183 /* Divide factor out until none are left. */ 184 do { 185 (void)printf(" %lu", (unsigned long) *fact); 186 val /= (long)*fact; 187 } while ((val % (long)*fact) == 0); 188 189 /* Let the user know we're doing something. */ 190 (void)fflush(stdout); 191 } 192 (void)putchar('\n'); 193 } 194 195 196 /* At this point, our number may have factors greater than those in primes[]; 197 * however, we can generate primes up to 32 bits (see primes(6)), which is 198 * sufficient to factor a 64-bit quad. 199 */ 200 void 201 pr_bigfact(u_int64_t val) /* Factor this value. */ 202 { 203 ubig start, stop, factor; 204 char *q; 205 const ubig *p; 206 ubig fact_lim, mod; 207 char *tab_lim; 208 char table[TABSIZE]; /* Eratosthenes sieve of odd numbers */ 209 210 start = *pr_limit + 2; 211 stop = (ubig)sqrt((double)val); 212 if ((stop & 0x1) == 0) 213 stop++; 214 /* 215 * Following code barely modified from that in primes(6) 216 * 217 * we shall sieve a bytemap window, note primes and move the window 218 * upward until we pass the stop point 219 */ 220 while (start < stop) { 221 /* 222 * factor out 3, 5, 7, 11 and 13 223 */ 224 /* initial pattern copy */ 225 factor = (start%(2*3*5*7*11*13))/2; /* starting copy spot */ 226 memcpy(table, &pattern[factor], pattern_size-factor); 227 /* main block pattern copies */ 228 for (fact_lim = pattern_size - factor; 229 fact_lim + pattern_size <= TABSIZE; fact_lim += pattern_size) { 230 memcpy(&table[fact_lim], pattern, pattern_size); 231 } 232 /* final block pattern copy */ 233 memcpy(&table[fact_lim], pattern, TABSIZE-fact_lim); 234 235 if (stop-start > TABSIZE+TABSIZE) { 236 tab_lim = &table[TABSIZE]; /* sieve it all */ 237 fact_lim = (int)sqrt( 238 (double)(start)+TABSIZE+TABSIZE+1.0); 239 } else { 240 tab_lim = &table[(stop - start)/2]; /* partial sieve */ 241 fact_lim = (int)sqrt((double)(stop) + 1.0); 242 } 243 /* sieve for factors >= 17 */ 244 factor = 17; /* 17 is first prime to use */ 245 p = &prime[7]; /* 19 is next prime, pi(19)=7 */ 246 do { 247 /* determine the factor's initial sieve point */ 248 mod = start % factor; 249 if (mod & 0x1) 250 q = &table[(factor-mod)/2]; 251 else 252 q = &table[mod ? factor-(mod/2) : 0]; 253 /* sieve for our current factor */ 254 for ( ; q < tab_lim; q += factor) { 255 *q = '\0'; /* sieve out a spot */ 256 } 257 } while ((factor=(ubig)(*(p++))) <= fact_lim); 258 259 /* 260 * use generated primes 261 */ 262 for (q = table; q < tab_lim; ++q, start+=2) { 263 if (*q) { 264 if (val % start == 0) { 265 do { 266 (void)printf(" %lu", (unsigned long) start); 267 val /= start; 268 } while ((val % start) == 0); 269 (void)fflush(stdout); 270 stop = (ubig)sqrt((double)val); 271 if ((stop & 0x1) == 0) 272 stop++; 273 } 274 } 275 } 276 } 277 if (val > 1) 278 printf(" %llu", val); 279 } 280 281 282 void 283 usage(void) 284 { 285 (void)fprintf(stderr, "usage: factor [number ...]\n"); 286 exit (1); 287 } 288