1 /* $OpenBSD: factor.c,v 1.14 2003/06/03 03:01:39 millert 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 #ifndef lint 37 static char copyright[] = 38 "@(#) Copyright (c) 1989, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)factor.c 8.4 (Berkeley) 5/4/95"; 45 #else 46 static char rcsid[] = "$OpenBSD: factor.c,v 1.14 2003/06/03 03:01:39 millert Exp $"; 47 #endif 48 #endif /* not lint */ 49 50 /* 51 * factor - factor a number into primes 52 * 53 * By: Landon Curt Noll chongo@toad.com, ...!{sun,tolsoft}!hoptoad!chongo 54 * 55 * chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\ 56 * 57 * usage: 58 * factor [number] ... 59 * 60 * The form of the output is: 61 * 62 * number: factor1 factor1 factor2 factor3 factor3 factor3 ... 63 * 64 * where factor1 < factor2 < factor3 < ... 65 * 66 * If no args are given, the list of numbers are read from stdin. 67 */ 68 69 #include <sys/types.h> 70 #include <err.h> 71 #include <ctype.h> 72 #include <errno.h> 73 #include <limits.h> 74 #include <math.h> 75 #include <stdio.h> 76 #include <stdlib.h> 77 #include <string.h> 78 #include <unistd.h> 79 80 #include "primes.h" 81 82 /* 83 * prime[i] is the (i+1)th prime. 84 * 85 * We are able to sieve 2^32-1 because this byte table yields all primes 86 * up to 65537 and 65537^2 > 2^32-1. 87 */ 88 extern const ubig prime[]; 89 extern const ubig *pr_limit; /* largest prime in the prime array */ 90 extern const char pattern[]; 91 extern const int pattern_size; 92 93 void pr_fact(u_int64_t); /* print factors of a value */ 94 void pr_bigfact(u_int64_t); 95 void usage(void); 96 97 int 98 main(argc, argv) 99 int argc; 100 char *argv[]; 101 { 102 u_int64_t val; 103 int ch; 104 char *p, buf[100]; /* > max number of digits. */ 105 106 while ((ch = getopt(argc, argv, "")) != -1) 107 switch (ch) { 108 case '?': 109 default: 110 usage(); 111 } 112 argc -= optind; 113 argv += optind; 114 115 /* No args supplied, read numbers from stdin. */ 116 if (argc == 0) 117 for (;;) { 118 if (fgets(buf, sizeof(buf), stdin) == NULL) { 119 if (ferror(stdin)) 120 err(1, "stdin"); 121 exit (0); 122 } 123 if (*(p = buf + strlen(buf) - 1) == '\n') 124 *p = '\0'; 125 for (p = buf; isblank(*p); ++p); 126 if (*p == '\0') 127 continue; 128 if (*p == '-') 129 errx(1, "negative numbers aren't permitted."); 130 errno = 0; 131 val = strtouq(buf, &p, 10); 132 if (errno) 133 err(1, "%s", buf); 134 for (; isblank(*p); ++p); 135 if (*p != '\0') 136 errx(1, "%s: illegal numeric format.", buf); 137 pr_fact(val); 138 } 139 /* Factor the arguments. */ 140 else 141 for (; *argv != NULL; ++argv) { 142 if (argv[0][0] == '-') 143 errx(1, "negative numbers aren't permitted."); 144 errno = 0; 145 val = strtouq(argv[0], &p, 10); 146 if (errno) 147 err(1, "%s", argv[0]); 148 if (*p != '\0') 149 errx(1, "%s: illegal numeric format.", argv[0]); 150 pr_fact(val); 151 } 152 exit(0); 153 } 154 155 /* 156 * pr_fact - print the factors of a number 157 * 158 * If the number is 0 or 1, then print the number and return. 159 * If the number is < 0, print -1, negate the number and continue 160 * processing. 161 * 162 * Print the factors of the number, from the lowest to the highest. 163 * A factor will be printed multiple times if it divides the value 164 * multiple times. 165 * 166 * Factors are printed with leading tabs. 167 */ 168 void 169 pr_fact(val) 170 u_int64_t val; /* Factor this value. */ 171 { 172 const ubig *fact; /* The factor found. */ 173 174 /* Firewall - catch 0 and 1. */ 175 if (val == 0) /* Historical practice; 0 just exits. */ 176 exit(0); 177 if (val == 1) { 178 (void)printf("1: 1\n"); 179 return; 180 } 181 182 /* Factor value. */ 183 (void)printf("%llu:", val); 184 fflush(stdout); 185 for (fact = &prime[0]; val > 1; ++fact) { 186 /* Look for the smallest factor. */ 187 do { 188 if (val % (long)*fact == 0) 189 break; 190 } while (++fact <= pr_limit); 191 192 /* Watch for primes larger than the table. */ 193 if (fact > pr_limit) { 194 if (val > BIG) 195 pr_bigfact(val); 196 else 197 (void)printf(" %llu", val); 198 break; 199 } 200 201 /* Divide factor out until none are left. */ 202 do { 203 (void)printf(" %lu", (unsigned long) *fact); 204 val /= (long)*fact; 205 } while ((val % (long)*fact) == 0); 206 207 /* Let the user know we're doing something. */ 208 (void)fflush(stdout); 209 } 210 (void)putchar('\n'); 211 } 212 213 214 /* At this point, our number may have factors greater than those in primes[]; 215 * however, we can generate primes up to 32 bits (see primes(6)), which is 216 * sufficient to factor a 64-bit quad. 217 */ 218 void 219 pr_bigfact(val) 220 u_int64_t val; /* Factor this value. */ 221 { 222 ubig start, stop, factor; 223 char *q; 224 const ubig *p; 225 ubig fact_lim, mod; 226 char *tab_lim; 227 char table[TABSIZE]; /* Eratosthenes sieve of odd numbers */ 228 229 start = *pr_limit + 2; 230 stop = (ubig)sqrt((double)val); 231 if ((stop & 0x1) == 0) 232 stop++; 233 /* 234 * Following code barely modified from that in primes(6) 235 * 236 * we shall sieve a bytemap window, note primes and move the window 237 * upward until we pass the stop point 238 */ 239 while (start < stop) { 240 /* 241 * factor out 3, 5, 7, 11 and 13 242 */ 243 /* initial pattern copy */ 244 factor = (start%(2*3*5*7*11*13))/2; /* starting copy spot */ 245 memcpy(table, &pattern[factor], pattern_size-factor); 246 /* main block pattern copies */ 247 for (fact_lim = pattern_size - factor; 248 fact_lim + pattern_size <= TABSIZE; fact_lim += pattern_size) { 249 memcpy(&table[fact_lim], pattern, pattern_size); 250 } 251 /* final block pattern copy */ 252 memcpy(&table[fact_lim], pattern, TABSIZE-fact_lim); 253 254 if (stop-start > TABSIZE+TABSIZE) { 255 tab_lim = &table[TABSIZE]; /* sieve it all */ 256 fact_lim = (int)sqrt( 257 (double)(start)+TABSIZE+TABSIZE+1.0); 258 } else { 259 tab_lim = &table[(stop - start)/2]; /* partial sieve */ 260 fact_lim = (int)sqrt((double)(stop) + 1.0); 261 } 262 /* sieve for factors >= 17 */ 263 factor = 17; /* 17 is first prime to use */ 264 p = &prime[7]; /* 19 is next prime, pi(19)=7 */ 265 do { 266 /* determine the factor's initial sieve point */ 267 mod = start % factor; 268 if (mod & 0x1) 269 q = &table[(factor-mod)/2]; 270 else 271 q = &table[mod ? factor-(mod/2) : 0]; 272 /* sieve for our current factor */ 273 for ( ; q < tab_lim; q += factor) { 274 *q = '\0'; /* sieve out a spot */ 275 } 276 } while ((factor=(ubig)(*(p++))) <= fact_lim); 277 278 /* 279 * use generated primes 280 */ 281 for (q = table; q < tab_lim; ++q, start+=2) { 282 if (*q) { 283 if (val % start == 0) { 284 do { 285 (void)printf(" %lu", (unsigned long) start); 286 val /= start; 287 } while ((val % start) == 0); 288 (void)fflush(stdout); 289 stop = (ubig)sqrt((double)val); 290 if ((stop & 0x1) == 0) 291 stop++; 292 } 293 } 294 } 295 } 296 if (val > 1) 297 printf(" %llu", val); 298 } 299 300 301 void 302 usage() 303 { 304 (void)fprintf(stderr, "usage: factor [value ...]\n"); 305 exit (0); 306 } 307