1 /* factor -- print factors of n. lose if n > 2^32.
2 Copyright (C) 1986 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by Paul Rubin <phr@ocf.berkeley.edu>. */
26 register unsigned long n = n0, d;
36 /* The exit condition in the following loop is correct because
37 any time it is tested one of these 3 conditions holds:
40 (3) n is composite but has no factors less than d.
41 If (1) or (2) obviously the right thing happens.
42 If (3), then since n is composite it is >= d^2. */
43 for (d = 3; d * d <= n; d += 2)
47 printf ("\t%ld\n", d);
51 if (n != 1 || n0 == 1)
52 printf ("\t%ld\n", n);
62 /* FIXME: Use getline. */
63 if (fgets (buf, sizeof buf, stdin) == 0)
65 /* FIXME: Use strtoul. */
66 factor ((unsigned long) atoi (buf));
78 factor ((unsigned) atoi (argv[1]));
81 fprintf (stderr, "Usage: %s [number]\n", argv[0]);