Home | History | Annotate | Line # | Download | only in sysinst
factor.c revision 1.2
      1  1.2    rillig /*	$NetBSD: factor.c,v 1.2 2021/01/31 20:51:04 rillig 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 #include <sys/cdefs.h>
     36  1.1  dholland #include <stdio.h>
     37  1.1  dholland 
     38  1.1  dholland 
     39  1.1  dholland /*
     40  1.1  dholland  * primes - prime table, built to include up to 46345 because
     41  1.1  dholland  * sqrt(2^31) = 46340.9500118415
     42  1.1  dholland  *
     43  1.1  dholland  * building the table instead of storing a precomputed table saves
     44  1.1  dholland  * about 19K of space on the binary image.
     45  1.1  dholland  */
     46  1.1  dholland 
     47  1.1  dholland #ifdef TESTING
     48  1.1  dholland long primes[4800];
     49  1.1  dholland int  num_primes = 2;
     50  1.1  dholland 
     51  1.1  dholland static void build_primes (long max);
     52  1.1  dholland void factor (long val, long *fact_list, int fact_size, int *num_fact);
     53  1.1  dholland 
     54  1.1  dholland static void
     55  1.2    rillig build_primes(long max)
     56  1.1  dholland {
     57  1.1  dholland 	long pc;
     58  1.1  dholland 	int j;
     59  1.1  dholland 	long rem;
     60  1.1  dholland 
     61  1.1  dholland 	/*
     62  1.1  dholland 	 * Initialise primes at run-time rather than compile time
     63  1.1  dholland 	 * so it's put in bss rather than data.
     64  1.1  dholland 	 */
     65  1.1  dholland 	primes[0] = 2;
     66  1.1  dholland 	primes[1] = 3;
     67  1.1  dholland 
     68  1.1  dholland 	for (pc = primes[num_primes-1]; pc < 46345 && pc*pc <= max; pc+=2) {
     69  1.1  dholland 		j = 0;
     70  1.1  dholland 		rem = 1;
     71  1.1  dholland 		while (j < num_primes && primes[j] * primes[j] <= pc) {
     72  1.1  dholland 			if ((rem = pc % primes[j]) == 0)
     73  1.1  dholland 				break;
     74  1.1  dholland 			j++;
     75  1.1  dholland 		}
     76  1.1  dholland 		if (rem)
     77  1.1  dholland 			primes[num_primes++] = pc;
     78  1.1  dholland 	}
     79  1.1  dholland }
     80  1.1  dholland 
     81  1.1  dholland /* factor:  prepare a list of prime factors of val.
     82  1.2    rillig    The last number may not be a prime factor if the list is not
     83  1.1  dholland    long enough. */
     84  1.1  dholland 
     85  1.1  dholland void
     86  1.2    rillig factor(long val, long *fact_list, int fact_size, int *num_fact)
     87  1.1  dholland {
     88  1.1  dholland 	int i;
     89  1.1  dholland 
     90  1.1  dholland 	/* Check to make sure we have enough primes. */
     91  1.1  dholland 	build_primes(val);
     92  1.1  dholland 
     93  1.1  dholland 	i = 0;
     94  1.1  dholland 	*num_fact = 0;
     95  1.1  dholland 	while (*num_fact < fact_size-1 && val > 1 && i < num_primes) {
     96  1.1  dholland 		/* Find the next prime that divides. */
     97  1.1  dholland 		while (i < num_primes && val % primes[i] != 0) i++;
     98  1.1  dholland 
     99  1.1  dholland 		/* Put factors in array. */
    100  1.1  dholland 		while (*num_fact < fact_size-1 && i < num_primes &&
    101  1.1  dholland 		    val % primes[i] == 0) {
    102  1.1  dholland 			fact_list[(*num_fact)++] = primes[i];
    103  1.1  dholland 			val /= primes[i];
    104  1.1  dholland 		}
    105  1.1  dholland 	}
    106  1.1  dholland 	if (val > 1)
    107  1.1  dholland 		fact_list[(*num_fact)++] = val;
    108  1.1  dholland }
    109  1.1  dholland 
    110  1.1  dholland 
    111  1.1  dholland 
    112  1.1  dholland #include <stdio.h>
    113  1.1  dholland #include <stdlib.h>
    114  1.1  dholland 
    115  1.1  dholland int
    116  1.1  dholland main(int argc, char **argv)
    117  1.1  dholland {
    118  1.1  dholland 	long facts[30];
    119  1.1  dholland 	long val;
    120  1.1  dholland 	int i, nfact;
    121  1.1  dholland 	int arg;
    122  1.1  dholland 
    123  1.1  dholland 	if (argc < 2) {
    124  1.1  dholland 		fprintf (stderr, "usage: %s numbers\n", argv[0]);
    125  1.1  dholland 		exit(1);
    126  1.1  dholland 	}
    127  1.1  dholland 
    128  1.1  dholland 	/* Factor each arg! */
    129  1.1  dholland 	for (arg = 1; arg < argc; arg++) {
    130  1.1  dholland 
    131  1.1  dholland 		val = atol(argv[arg]);
    132  1.1  dholland 		factor (val, facts, 30, &nfact);
    133  1.1  dholland 
    134  1.1  dholland 		printf ("%ld:", val);
    135  1.1  dholland 		for (i = 0; i<nfact; i++)
    136  1.1  dholland 			printf (" %ld", facts[i]);
    137  1.1  dholland 		printf ("\n");
    138  1.1  dholland 
    139  1.1  dholland 	}
    140  1.1  dholland 
    141  1.1  dholland 	return 0;
    142  1.1  dholland }
    143  1.1  dholland #endif
    144