factor.c revision 1.1 1 1.1 dholland /* $NetBSD: factor.c,v 1.1 2014/07/26 19:30:44 dholland Exp $ */
2 1.1 dholland
3 1.1 dholland /*
4 1.1 dholland * Copyright 1997 Piermont Information Systems Inc.
5 1.1 dholland * All rights reserved.
6 1.1 dholland *
7 1.1 dholland * Written by Philip A. Nelson for Piermont Information Systems Inc.
8 1.1 dholland *
9 1.1 dholland * Redistribution and use in source and binary forms, with or without
10 1.1 dholland * modification, are permitted provided that the following conditions
11 1.1 dholland * are met:
12 1.1 dholland * 1. Redistributions of source code must retain the above copyright
13 1.1 dholland * notice, this list of conditions and the following disclaimer.
14 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 dholland * notice, this list of conditions and the following disclaimer in the
16 1.1 dholland * documentation and/or other materials provided with the distribution.
17 1.1 dholland * 3. The name of Piermont Information Systems Inc. may not be used to endorse
18 1.1 dholland * or promote products derived from this software without specific prior
19 1.1 dholland * written permission.
20 1.1 dholland *
21 1.1 dholland * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
22 1.1 dholland * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
25 1.1 dholland * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 dholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 dholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 dholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 dholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 dholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 1.1 dholland * THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 dholland *
33 1.1 dholland */
34 1.1 dholland
35 1.1 dholland /* Prototypes for strict prototyping. */
36 1.1 dholland
37 1.1 dholland #include <sys/cdefs.h>
38 1.1 dholland
39 1.1 dholland #include <stdio.h>
40 1.1 dholland
41 1.1 dholland
42 1.1 dholland /*
43 1.1 dholland * primes - prime table, built to include up to 46345 because
44 1.1 dholland * sqrt(2^31) = 46340.9500118415
45 1.1 dholland *
46 1.1 dholland * building the table instead of storing a precomputed table saves
47 1.1 dholland * about 19K of space on the binary image.
48 1.1 dholland */
49 1.1 dholland
50 1.1 dholland #ifdef TESTING
51 1.1 dholland long primes[4800];
52 1.1 dholland int num_primes = 2;
53 1.1 dholland
54 1.1 dholland static void build_primes (long max);
55 1.1 dholland void factor (long val, long *fact_list, int fact_size, int *num_fact);
56 1.1 dholland
57 1.1 dholland static void
58 1.1 dholland build_primes(max)
59 1.1 dholland long max;
60 1.1 dholland {
61 1.1 dholland long pc;
62 1.1 dholland int j;
63 1.1 dholland long rem;
64 1.1 dholland
65 1.1 dholland /*
66 1.1 dholland * Initialise primes at run-time rather than compile time
67 1.1 dholland * so it's put in bss rather than data.
68 1.1 dholland */
69 1.1 dholland primes[0] = 2;
70 1.1 dholland primes[1] = 3;
71 1.1 dholland
72 1.1 dholland for (pc = primes[num_primes-1]; pc < 46345 && pc*pc <= max; pc+=2) {
73 1.1 dholland j = 0;
74 1.1 dholland rem = 1;
75 1.1 dholland while (j < num_primes && primes[j] * primes[j] <= pc) {
76 1.1 dholland if ((rem = pc % primes[j]) == 0)
77 1.1 dholland break;
78 1.1 dholland j++;
79 1.1 dholland }
80 1.1 dholland if (rem)
81 1.1 dholland primes[num_primes++] = pc;
82 1.1 dholland }
83 1.1 dholland }
84 1.1 dholland
85 1.1 dholland /* factor: prepare a list of prime factors of val.
86 1.1 dholland The last number may not be a prime factor is the list is not
87 1.1 dholland long enough. */
88 1.1 dholland
89 1.1 dholland void
90 1.1 dholland factor(val, fact_list, fact_size, num_fact)
91 1.1 dholland long val;
92 1.1 dholland long *fact_list;
93 1.1 dholland int fact_size;
94 1.1 dholland int *num_fact;
95 1.1 dholland {
96 1.1 dholland int i;
97 1.1 dholland
98 1.1 dholland /* Check to make sure we have enough primes. */
99 1.1 dholland build_primes(val);
100 1.1 dholland
101 1.1 dholland i = 0;
102 1.1 dholland *num_fact = 0;
103 1.1 dholland while (*num_fact < fact_size-1 && val > 1 && i < num_primes) {
104 1.1 dholland /* Find the next prime that divides. */
105 1.1 dholland while (i < num_primes && val % primes[i] != 0) i++;
106 1.1 dholland
107 1.1 dholland /* Put factors in array. */
108 1.1 dholland while (*num_fact < fact_size-1 && i < num_primes &&
109 1.1 dholland val % primes[i] == 0) {
110 1.1 dholland fact_list[(*num_fact)++] = primes[i];
111 1.1 dholland val /= primes[i];
112 1.1 dholland }
113 1.1 dholland }
114 1.1 dholland if (val > 1)
115 1.1 dholland fact_list[(*num_fact)++] = val;
116 1.1 dholland }
117 1.1 dholland
118 1.1 dholland
119 1.1 dholland
120 1.1 dholland #include <stdio.h>
121 1.1 dholland #include <stdlib.h>
122 1.1 dholland
123 1.1 dholland int
124 1.1 dholland main(int argc, char **argv)
125 1.1 dholland {
126 1.1 dholland long facts[30];
127 1.1 dholland long val;
128 1.1 dholland int i, nfact;
129 1.1 dholland int arg;
130 1.1 dholland
131 1.1 dholland if (argc < 2) {
132 1.1 dholland fprintf (stderr, "usage: %s numbers\n", argv[0]);
133 1.1 dholland exit(1);
134 1.1 dholland }
135 1.1 dholland
136 1.1 dholland /* Factor each arg! */
137 1.1 dholland for (arg = 1; arg < argc; arg++) {
138 1.1 dholland
139 1.1 dholland val = atol(argv[arg]);
140 1.1 dholland factor (val, facts, 30, &nfact);
141 1.1 dholland
142 1.1 dholland printf ("%ld:", val);
143 1.1 dholland for (i = 0; i<nfact; i++)
144 1.1 dholland printf (" %ld", facts[i]);
145 1.1 dholland printf ("\n");
146 1.1 dholland
147 1.1 dholland }
148 1.1 dholland
149 1.1 dholland return 0;
150 1.1 dholland }
151 1.1 dholland #endif
152