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