1*002fd351Srillig /* $NetBSD: factor.c,v 1.2 2021/01/31 20:51:04 rillig Exp $ */
250dbef1aSdholland
350dbef1aSdholland /*
450dbef1aSdholland * Copyright 1997 Piermont Information Systems Inc.
550dbef1aSdholland * All rights reserved.
650dbef1aSdholland *
750dbef1aSdholland * Written by Philip A. Nelson for Piermont Information Systems Inc.
850dbef1aSdholland *
950dbef1aSdholland * Redistribution and use in source and binary forms, with or without
1050dbef1aSdholland * modification, are permitted provided that the following conditions
1150dbef1aSdholland * are met:
1250dbef1aSdholland * 1. Redistributions of source code must retain the above copyright
1350dbef1aSdholland * notice, this list of conditions and the following disclaimer.
1450dbef1aSdholland * 2. Redistributions in binary form must reproduce the above copyright
1550dbef1aSdholland * notice, this list of conditions and the following disclaimer in the
1650dbef1aSdholland * documentation and/or other materials provided with the distribution.
1750dbef1aSdholland * 3. The name of Piermont Information Systems Inc. may not be used to endorse
1850dbef1aSdholland * or promote products derived from this software without specific prior
1950dbef1aSdholland * written permission.
2050dbef1aSdholland *
2150dbef1aSdholland * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
2250dbef1aSdholland * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2350dbef1aSdholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2450dbef1aSdholland * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
2550dbef1aSdholland * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2650dbef1aSdholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2750dbef1aSdholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2850dbef1aSdholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2950dbef1aSdholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3050dbef1aSdholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3150dbef1aSdholland * THE POSSIBILITY OF SUCH DAMAGE.
3250dbef1aSdholland *
3350dbef1aSdholland */
3450dbef1aSdholland
3550dbef1aSdholland #include <sys/cdefs.h>
3650dbef1aSdholland #include <stdio.h>
3750dbef1aSdholland
3850dbef1aSdholland
3950dbef1aSdholland /*
4050dbef1aSdholland * primes - prime table, built to include up to 46345 because
4150dbef1aSdholland * sqrt(2^31) = 46340.9500118415
4250dbef1aSdholland *
4350dbef1aSdholland * building the table instead of storing a precomputed table saves
4450dbef1aSdholland * about 19K of space on the binary image.
4550dbef1aSdholland */
4650dbef1aSdholland
4750dbef1aSdholland #ifdef TESTING
4850dbef1aSdholland long primes[4800];
4950dbef1aSdholland int num_primes = 2;
5050dbef1aSdholland
5150dbef1aSdholland static void build_primes (long max);
5250dbef1aSdholland void factor (long val, long *fact_list, int fact_size, int *num_fact);
5350dbef1aSdholland
5450dbef1aSdholland static void
build_primes(long max)55*002fd351Srillig build_primes(long max)
5650dbef1aSdholland {
5750dbef1aSdholland long pc;
5850dbef1aSdholland int j;
5950dbef1aSdholland long rem;
6050dbef1aSdholland
6150dbef1aSdholland /*
6250dbef1aSdholland * Initialise primes at run-time rather than compile time
6350dbef1aSdholland * so it's put in bss rather than data.
6450dbef1aSdholland */
6550dbef1aSdholland primes[0] = 2;
6650dbef1aSdholland primes[1] = 3;
6750dbef1aSdholland
6850dbef1aSdholland for (pc = primes[num_primes-1]; pc < 46345 && pc*pc <= max; pc+=2) {
6950dbef1aSdholland j = 0;
7050dbef1aSdholland rem = 1;
7150dbef1aSdholland while (j < num_primes && primes[j] * primes[j] <= pc) {
7250dbef1aSdholland if ((rem = pc % primes[j]) == 0)
7350dbef1aSdholland break;
7450dbef1aSdholland j++;
7550dbef1aSdholland }
7650dbef1aSdholland if (rem)
7750dbef1aSdholland primes[num_primes++] = pc;
7850dbef1aSdholland }
7950dbef1aSdholland }
8050dbef1aSdholland
8150dbef1aSdholland /* factor: prepare a list of prime factors of val.
82*002fd351Srillig The last number may not be a prime factor if the list is not
8350dbef1aSdholland long enough. */
8450dbef1aSdholland
8550dbef1aSdholland void
factor(long val,long * fact_list,int fact_size,int * num_fact)86*002fd351Srillig factor(long val, long *fact_list, int fact_size, int *num_fact)
8750dbef1aSdholland {
8850dbef1aSdholland int i;
8950dbef1aSdholland
9050dbef1aSdholland /* Check to make sure we have enough primes. */
9150dbef1aSdholland build_primes(val);
9250dbef1aSdholland
9350dbef1aSdholland i = 0;
9450dbef1aSdholland *num_fact = 0;
9550dbef1aSdholland while (*num_fact < fact_size-1 && val > 1 && i < num_primes) {
9650dbef1aSdholland /* Find the next prime that divides. */
9750dbef1aSdholland while (i < num_primes && val % primes[i] != 0) i++;
9850dbef1aSdholland
9950dbef1aSdholland /* Put factors in array. */
10050dbef1aSdholland while (*num_fact < fact_size-1 && i < num_primes &&
10150dbef1aSdholland val % primes[i] == 0) {
10250dbef1aSdholland fact_list[(*num_fact)++] = primes[i];
10350dbef1aSdholland val /= primes[i];
10450dbef1aSdholland }
10550dbef1aSdholland }
10650dbef1aSdholland if (val > 1)
10750dbef1aSdholland fact_list[(*num_fact)++] = val;
10850dbef1aSdholland }
10950dbef1aSdholland
11050dbef1aSdholland
11150dbef1aSdholland
11250dbef1aSdholland #include <stdio.h>
11350dbef1aSdholland #include <stdlib.h>
11450dbef1aSdholland
11550dbef1aSdholland int
main(int argc,char ** argv)11650dbef1aSdholland main(int argc, char **argv)
11750dbef1aSdholland {
11850dbef1aSdholland long facts[30];
11950dbef1aSdholland long val;
12050dbef1aSdholland int i, nfact;
12150dbef1aSdholland int arg;
12250dbef1aSdholland
12350dbef1aSdholland if (argc < 2) {
12450dbef1aSdholland fprintf (stderr, "usage: %s numbers\n", argv[0]);
12550dbef1aSdholland exit(1);
12650dbef1aSdholland }
12750dbef1aSdholland
12850dbef1aSdholland /* Factor each arg! */
12950dbef1aSdholland for (arg = 1; arg < argc; arg++) {
13050dbef1aSdholland
13150dbef1aSdholland val = atol(argv[arg]);
13250dbef1aSdholland factor (val, facts, 30, &nfact);
13350dbef1aSdholland
13450dbef1aSdholland printf ("%ld:", val);
13550dbef1aSdholland for (i = 0; i<nfact; i++)
13650dbef1aSdholland printf (" %ld", facts[i]);
13750dbef1aSdholland printf ("\n");
13850dbef1aSdholland
13950dbef1aSdholland }
14050dbef1aSdholland
14150dbef1aSdholland return 0;
14250dbef1aSdholland }
14350dbef1aSdholland #endif
144