1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29
30 /* factor COMPILE: cc -O factor.c -s -i -lm -o factor */
31 /*
32 * works up to 14 digit numbers
33 * running time is proportional to sqrt(n)
34 * accepts arguments either as input or on command line
35 * 0 input terminates processing
36 */
37
38 double modf(), sqrt();
39 double nn, vv;
40 double huge = 1.0e14;
41 double sq[] = {
42 10, 2, 4, 2, 4, 6, 2, 6,
43 4, 2, 4, 6, 6, 2, 6, 4,
44 2, 6, 4, 6, 8, 4, 2, 4,
45 2, 4, 8, 6, 4, 6, 2, 4,
46 6, 2, 6, 6, 4, 2, 4, 6,
47 2, 6, 4, 2, 4, 2,10, 2,
48 };
49
50 void try(double);
51
52 int
main(int argc,char * argv[])53 main(int argc, char *argv[])
54 {
55 int test = 1;
56 int ret;
57 int j;
58 double junk, temp;
59 double fr;
60 double ii;
61
62 if(argc > 2){
63 printf("Usage: factor number\n");
64 exit(1);
65 }
66 if(argc == 2){
67 ret = sscanf(argv[1], "%lf", &nn);
68 test = 0;
69 printf("%.0f\n", nn);
70 goto start;
71 }
72 while(test == 1){
73 ret = scanf("%lf", &nn);
74 start:
75 if((ret<1) || (nn == 0.0)){
76 exit(0);
77 }
78 if((nn<0.0) || (nn>huge)){
79 printf("Ouch!\n");
80 continue;
81 }
82 fr = modf(nn, &junk);
83 if(fr != 0.0){
84 printf("Not an integer!\n");
85 continue;
86 }
87 vv = 1. + sqrt(nn);
88 try(2.0);
89 try(3.0);
90 try(5.0);
91 try(7.0);
92 ii = 1.0;
93 while(ii <= vv){
94 for(j=0; j<48; j++){
95 ii += sq[j];
96 retry:
97 modf(nn/ii, &temp);
98 if(nn == temp*ii){
99 printf(" %.0f\n", ii);
100 nn = nn/ii;
101 vv = 1 + sqrt(nn);
102 goto retry;
103 }
104 }
105 }
106 if(nn > 1.0){
107 printf(" %.0f\n", nn);
108 }
109 printf("\n");
110 }
111 return (0);
112 }
113
114 void
try(double arg)115 try(double arg)
116 {
117 double temp;
118 retry:
119 modf(nn/arg, &temp);
120 if(nn == temp*arg){
121 printf(" %.0f\n", arg);
122 nn = nn/arg;
123 vv = 1 + sqrt(nn);
124 goto retry;
125 }
126 }
127