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