1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-04-20 18:56:39 +02:00
Files
coreutils/src/factor.c

173 lines
3.8 KiB
C
Raw Normal View History

1994-12-12 17:49:55 +00:00
/* factor -- print factors of n. lose if n > 2^32.
1995-03-09 22:33:05 +00:00
Copyright (C) 86, 1995 Free Software Foundation, Inc.
1994-12-12 17:49:55 +00:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/* Written by Paul Rubin <phr@ocf.berkeley.edu>. */
1994-12-20 05:24:13 +00:00
#include <config.h>
1994-12-12 17:49:55 +00:00
#include <stdio.h>
1994-12-20 05:24:13 +00:00
#include <sys/types.h>
1994-12-31 17:33:56 +00:00
#include <assert.h>
#define NDEBUG 1
1994-12-20 05:24:13 +00:00
#include "system.h"
#include "version.h"
#include "long-options.h"
#include "error.h"
1994-12-31 15:28:51 +00:00
#include "readtokens.h"
1994-12-31 17:33:56 +00:00
/* Token delimiters when reading from a file. */
1994-12-31 15:28:51 +00:00
#define DELIM "\n\t "
1994-12-20 05:24:13 +00:00
1994-12-31 17:33:56 +00:00
/* FIXME: if this program is ever modified to factor integers larger
than 2^128, this (and the algorithm :-) will have to change. */
#define MAX_N_FACTORS 128
1994-12-20 05:24:13 +00:00
/* The name this program was run with. */
char *program_name;
1994-12-12 17:49:55 +00:00
static void
1994-12-20 05:24:13 +00:00
usage (status)
int status;
{
if (status != 0)
fprintf (stderr, "Try `%s --help' for more information.\n",
program_name);
else
{
printf ("\
Usage: %s [NUMBER]\n\
or: %s OPTION\n\
",
program_name, program_name);
printf ("\
\n\
--help display this help and exit\n\
--version output version information and exit\n\
");
}
exit (status);
}
static int
factor (n0, max_n_factors, factors)
1994-12-12 17:49:55 +00:00
unsigned long n0;
1994-12-20 05:24:13 +00:00
int max_n_factors;
unsigned long *factors;
1994-12-12 17:49:55 +00:00
{
register unsigned long n = n0, d;
1994-12-20 05:24:13 +00:00
int n_factors = 0;
1994-12-12 17:49:55 +00:00
if (n < 1)
1994-12-20 05:24:13 +00:00
return n_factors;
1994-12-12 17:49:55 +00:00
while (n % 2 == 0)
{
1994-12-31 17:33:56 +00:00
assert (n_factors < max_n_factors);
1994-12-20 05:24:13 +00:00
factors[n_factors++] = 2;
1994-12-12 17:49:55 +00:00
n /= 2;
}
1994-12-20 05:24:13 +00:00
1994-12-12 17:49:55 +00:00
/* The exit condition in the following loop is correct because
any time it is tested one of these 3 conditions holds:
(1) d divides n
(2) n is prime
(3) n is composite but has no factors less than d.
If (1) or (2) obviously the right thing happens.
If (3), then since n is composite it is >= d^2. */
for (d = 3; d * d <= n; d += 2)
{
while (n % d == 0)
{
1994-12-31 17:33:56 +00:00
assert (n_factors < max_n_factors);
1994-12-20 05:24:13 +00:00
factors[n_factors++] = d;
1994-12-12 17:49:55 +00:00
n /= d;
}
}
if (n != 1 || n0 == 1)
1994-12-31 17:33:56 +00:00
{
assert (n_factors < max_n_factors);
factors[n_factors++] = n;
}
1994-12-20 05:24:13 +00:00
return n_factors;
}
static void
print_factors (n)
unsigned long int n;
{
1994-12-31 17:33:56 +00:00
unsigned long int factors[MAX_N_FACTORS];
1994-12-20 05:24:13 +00:00
int n_factors;
int i;
1994-12-31 17:33:56 +00:00
n_factors = factor (n, MAX_N_FACTORS, factors);
for (i = 0; i < n_factors; i++)
1994-12-31 15:28:51 +00:00
printf (" %lu\n", factors[i]);
putchar ('\n');
1994-12-12 17:49:55 +00:00
}
static void
1994-12-12 17:49:55 +00:00
do_stdin ()
{
1994-12-31 15:28:51 +00:00
token_buffer tokenbuffer;
init_tokenbuffer (&tokenbuffer);
1994-12-12 17:49:55 +00:00
for (;;)
{
1994-12-31 15:28:51 +00:00
long int token_length;
token_length = readtoken (stdin, DELIM, sizeof (DELIM) - 1,
&tokenbuffer);
if (token_length < 0)
break;
1994-12-31 17:56:47 +00:00
/* FIXME: Use xstrtoul, not atoi. */
1994-12-31 15:28:51 +00:00
print_factors ((unsigned long) atoi (tokenbuffer.buffer));
1994-12-12 17:49:55 +00:00
}
1994-12-31 15:28:51 +00:00
free (tokenbuffer.buffer);
1994-12-12 17:49:55 +00:00
}
void
main (argc, argv)
int argc;
char **argv;
{
1994-12-20 05:24:13 +00:00
program_name = argv[0];
parse_long_options (argc, argv, "factor", version_string, usage);
if (argc > 2)
{
error (0, 0, "too many arguments");
usage (1);
}
if (argc == 1)
do_stdin ();
else if (argc == 2)
1994-12-20 05:24:13 +00:00
{
print_factors ((unsigned long) atoi (argv[1]));
}
else
{
fprintf (stderr, "Usage: %s [number]\n", argv[0]);
exit (1);
}
exit (0);
}