1 /* $OpenBSD: factor.c,v 1.18 2008/03/17 09:17:56 sobrado 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.18 2008/03/17 09:17:56 sobrado 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(int argc, char *argv[]) 99 { 100 u_int64_t val; 101 int ch; 102 char *p, buf[100]; /* > max number of digits. */ 103 104 while ((ch = getopt(argc, argv, "")) != -1) 105 switch (ch) { 106 case '?': 107 default: 108 usage(); 109 } 110 argc -= optind; 111 argv += optind; 112 113 /* No args supplied, read numbers from stdin. */ 114 if (argc == 0) 115 for (;;) { 116 if (fgets(buf, sizeof(buf), stdin) == NULL) { 117 if (ferror(stdin)) 118 err(1, "stdin"); 119 exit (0); 120 } 121 buf[strcspn(buf, "\n")] = '\0'; 122 for (p = buf; isblank(*p); ++p); 123 if (*p == '\0') 124 continue; 125 if (*p == '-') 126 errx(1, "negative numbers aren't permitted."); 127 errno = 0; 128 val = strtouq(buf, &p, 10); 129 if (errno) 130 err(1, "%s", buf); 131 for (; isblank(*p); ++p); 132 if (*p != '\0') 133 errx(1, "%s: illegal numeric format.", buf); 134 pr_fact(val); 135 } 136 /* Factor the arguments. */ 137 else 138 for (; *argv != NULL; ++argv) { 139 if (argv[0][0] == '-') 140 errx(1, "negative numbers aren't permitted."); 141 errno = 0; 142 val = strtouq(argv[0], &p, 10); 143 if (errno) 144 err(1, "%s", argv[0]); 145 if (*p != '\0') 146 errx(1, "%s: illegal numeric format.", argv[0]); 147 pr_fact(val); 148 } 149 exit(0); 150 } 151 152 /* 153 * pr_fact - print the factors of a number 154 * 155 * If the number is 0 or 1, then print the number and return. 156 * If the number is < 0, print -1, negate the number and continue 157 * processing. 158 * 159 * Print the factors of the number, from the lowest to the highest. 160 * A factor will be printed multiple times if it divides the value 161 * multiple times. 162 * 163 * Factors are printed with leading tabs. 164 */ 165 void 166 pr_fact(u_int64_t val) /* Factor this value. */ 167 { 168 const ubig *fact; /* The factor found. */ 169 170 /* Firewall - catch 0 and 1. */ 171 if (val == 0) /* Historical practice; 0 just exits. */ 172 exit(0); 173 if (val == 1) { 174 (void)printf("1: 1\n"); 175 return; 176 } 177 178 /* Factor value. */ 179 (void)printf("%llu:", val); 180 fflush(stdout); 181 for (fact = &prime[0]; val > 1; ++fact) { 182 /* Look for the smallest factor. */ 183 do { 184 if (val % (long)*fact == 0) 185 break; 186 } while (++fact <= pr_limit); 187 188 /* Watch for primes larger than the table. */ 189 if (fact > pr_limit) { 190 if (val > BIG) 191 pr_bigfact(val); 192 else 193 (void)printf(" %llu", val); 194 break; 195 } 196 197 /* Divide factor out until none are left. */ 198 do { 199 (void)printf(" %lu", (unsigned long) *fact); 200 val /= (long)*fact; 201 } while ((val % (long)*fact) == 0); 202 203 /* Let the user know we're doing something. */ 204 (void)fflush(stdout); 205 } 206 (void)putchar('\n'); 207 } 208 209 210 /* At this point, our number may have factors greater than those in primes[]; 211 * however, we can generate primes up to 32 bits (see primes(6)), which is 212 * sufficient to factor a 64-bit quad. 213 */ 214 void 215 pr_bigfact(u_int64_t val) /* Factor this value. */ 216 { 217 ubig start, stop, factor; 218 char *q; 219 const ubig *p; 220 ubig fact_lim, mod; 221 char *tab_lim; 222 char table[TABSIZE]; /* Eratosthenes sieve of odd numbers */ 223 224 start = *pr_limit + 2; 225 stop = (ubig)sqrt((double)val); 226 if ((stop & 0x1) == 0) 227 stop++; 228 /* 229 * Following code barely modified from that in primes(6) 230 * 231 * we shall sieve a bytemap window, note primes and move the window 232 * upward until we pass the stop point 233 */ 234 while (start < stop) { 235 /* 236 * factor out 3, 5, 7, 11 and 13 237 */ 238 /* initial pattern copy */ 239 factor = (start%(2*3*5*7*11*13))/2; /* starting copy spot */ 240 memcpy(table, &pattern[factor], pattern_size-factor); 241 /* main block pattern copies */ 242 for (fact_lim = pattern_size - factor; 243 fact_lim + pattern_size <= TABSIZE; fact_lim += pattern_size) { 244 memcpy(&table[fact_lim], pattern, pattern_size); 245 } 246 /* final block pattern copy */ 247 memcpy(&table[fact_lim], pattern, TABSIZE-fact_lim); 248 249 if (stop-start > TABSIZE+TABSIZE) { 250 tab_lim = &table[TABSIZE]; /* sieve it all */ 251 fact_lim = (int)sqrt( 252 (double)(start)+TABSIZE+TABSIZE+1.0); 253 } else { 254 tab_lim = &table[(stop - start)/2]; /* partial sieve */ 255 fact_lim = (int)sqrt((double)(stop) + 1.0); 256 } 257 /* sieve for factors >= 17 */ 258 factor = 17; /* 17 is first prime to use */ 259 p = &prime[7]; /* 19 is next prime, pi(19)=7 */ 260 do { 261 /* determine the factor's initial sieve point */ 262 mod = start % factor; 263 if (mod & 0x1) 264 q = &table[(factor-mod)/2]; 265 else 266 q = &table[mod ? factor-(mod/2) : 0]; 267 /* sieve for our current factor */ 268 for ( ; q < tab_lim; q += factor) { 269 *q = '\0'; /* sieve out a spot */ 270 } 271 } while ((factor=(ubig)(*(p++))) <= fact_lim); 272 273 /* 274 * use generated primes 275 */ 276 for (q = table; q < tab_lim; ++q, start+=2) { 277 if (*q) { 278 if (val % start == 0) { 279 do { 280 (void)printf(" %lu", (unsigned long) start); 281 val /= start; 282 } while ((val % start) == 0); 283 (void)fflush(stdout); 284 stop = (ubig)sqrt((double)val); 285 if ((stop & 0x1) == 0) 286 stop++; 287 } 288 } 289 } 290 } 291 if (val > 1) 292 printf(" %llu", val); 293 } 294 295 296 void 297 usage(void) 298 { 299 (void)fprintf(stderr, "usage: factor [number ...]\n"); 300 exit (1); 301 } 302