xref: /titanic_52/usr/src/cmd/factor/factor.c (revision 3c112a2b34403220c06c3e2fcac403358cfba168)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*	factor	COMPILE:	cc -O factor.c -s -i -lm -o factor	*/
33 /*
34  * works up to 14 digit numbers
35  * running time is proportional to sqrt(n)
36  * accepts arguments either as input or on command line
37  * 0 input terminates processing
38  */
39 
40 double modf(), sqrt();
41 double nn, vv;
42 double huge = 1.0e14;
43 double sq[] = {
44 	10, 2, 4, 2, 4, 6, 2, 6,
45 	 4, 2, 4, 6, 6, 2, 6, 4,
46 	 2, 6, 4, 6, 8, 4, 2, 4,
47 	 2, 4, 8, 6, 4, 6, 2, 4,
48 	 6, 2, 6, 6, 4, 2, 4, 6,
49 	 2, 6, 4, 2, 4, 2,10, 2,
50 };
51 
52 void try(double);
53 
54 int
55 main(int argc, char *argv[])
56 {
57 	int test = 1;
58 	int ret;
59 	int j;
60 	double junk, temp;
61 	double fr;
62 	double ii;
63 
64 	if(argc > 2){
65 		printf("Usage: factor number\n");
66 		exit(1);
67 	}
68 	if(argc == 2){
69 		ret = sscanf(argv[1], "%lf", &nn);
70 		test = 0;
71 		printf("%.0f\n", nn);
72 		goto start;
73 	}
74 	while(test == 1){
75 		ret = scanf("%lf", &nn);
76 start:
77 		if((ret<1) || (nn == 0.0)){
78 			exit(0);
79 		}
80 		if((nn<0.0) || (nn>huge)){
81 			printf("Ouch!\n");
82 			continue;
83 		}
84 		fr = modf(nn, &junk);
85 		if(fr != 0.0){
86 			printf("Not an integer!\n");
87 			continue;
88 		}
89 		vv = 1. + sqrt(nn);
90 		try(2.0);
91 		try(3.0);
92 		try(5.0);
93 		try(7.0);
94 		ii = 1.0;
95 		while(ii <= vv){
96 			for(j=0; j<48; j++){
97 				ii += sq[j];
98 retry:
99 				modf(nn/ii, &temp);
100 				if(nn == temp*ii){
101 					printf("     %.0f\n", ii);
102 					nn = nn/ii;
103 					vv = 1 + sqrt(nn);
104 					goto retry;
105 				}
106 			}
107 		}
108 		if(nn > 1.0){
109 			printf("     %.0f\n", nn);
110 		}
111 		printf("\n");
112 	}
113 	return (0);
114 }
115 
116 void
117 try(double arg)
118 {
119 	double temp;
120 retry:
121 	modf(nn/arg, &temp);
122 	if(nn == temp*arg){
123 		printf("     %.0f\n", arg);
124 		nn = nn/arg;
125 		vv = 1 + sqrt(nn);
126 		goto retry;
127 	}
128 }
129