10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
22*239Sceastha /*
23*239Sceastha * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24*239Sceastha * Use is subject to license terms.
25*239Sceastha */
26*239Sceastha
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
30*239Sceastha #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
320Sstevel@tonic-gate /* factor COMPILE: cc -O factor.c -s -i -lm -o factor */
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate * works up to 14 digit numbers
350Sstevel@tonic-gate * running time is proportional to sqrt(n)
360Sstevel@tonic-gate * accepts arguments either as input or on command line
370Sstevel@tonic-gate * 0 input terminates processing
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate double modf(), sqrt();
410Sstevel@tonic-gate double nn, vv;
420Sstevel@tonic-gate double huge = 1.0e14;
430Sstevel@tonic-gate double sq[] = {
440Sstevel@tonic-gate 10, 2, 4, 2, 4, 6, 2, 6,
450Sstevel@tonic-gate 4, 2, 4, 6, 6, 2, 6, 4,
460Sstevel@tonic-gate 2, 6, 4, 6, 8, 4, 2, 4,
470Sstevel@tonic-gate 2, 4, 8, 6, 4, 6, 2, 4,
480Sstevel@tonic-gate 6, 2, 6, 6, 4, 2, 4, 6,
490Sstevel@tonic-gate 2, 6, 4, 2, 4, 2,10, 2,
500Sstevel@tonic-gate };
510Sstevel@tonic-gate
52*239Sceastha void try(double);
53*239Sceastha
54*239Sceastha int
main(int argc,char * argv[])55*239Sceastha main(int argc, char *argv[])
560Sstevel@tonic-gate {
570Sstevel@tonic-gate int test = 1;
580Sstevel@tonic-gate int ret;
59*239Sceastha int j;
600Sstevel@tonic-gate double junk, temp;
610Sstevel@tonic-gate double fr;
620Sstevel@tonic-gate double ii;
630Sstevel@tonic-gate
640Sstevel@tonic-gate if(argc > 2){
650Sstevel@tonic-gate printf("Usage: factor number\n");
660Sstevel@tonic-gate exit(1);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate if(argc == 2){
690Sstevel@tonic-gate ret = sscanf(argv[1], "%lf", &nn);
700Sstevel@tonic-gate test = 0;
710Sstevel@tonic-gate printf("%.0f\n", nn);
720Sstevel@tonic-gate goto start;
730Sstevel@tonic-gate }
740Sstevel@tonic-gate while(test == 1){
750Sstevel@tonic-gate ret = scanf("%lf", &nn);
760Sstevel@tonic-gate start:
770Sstevel@tonic-gate if((ret<1) || (nn == 0.0)){
780Sstevel@tonic-gate exit(0);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate if((nn<0.0) || (nn>huge)){
810Sstevel@tonic-gate printf("Ouch!\n");
820Sstevel@tonic-gate continue;
830Sstevel@tonic-gate }
840Sstevel@tonic-gate fr = modf(nn, &junk);
850Sstevel@tonic-gate if(fr != 0.0){
860Sstevel@tonic-gate printf("Not an integer!\n");
870Sstevel@tonic-gate continue;
880Sstevel@tonic-gate }
890Sstevel@tonic-gate vv = 1. + sqrt(nn);
900Sstevel@tonic-gate try(2.0);
910Sstevel@tonic-gate try(3.0);
920Sstevel@tonic-gate try(5.0);
930Sstevel@tonic-gate try(7.0);
940Sstevel@tonic-gate ii = 1.0;
950Sstevel@tonic-gate while(ii <= vv){
960Sstevel@tonic-gate for(j=0; j<48; j++){
970Sstevel@tonic-gate ii += sq[j];
980Sstevel@tonic-gate retry:
990Sstevel@tonic-gate modf(nn/ii, &temp);
1000Sstevel@tonic-gate if(nn == temp*ii){
1010Sstevel@tonic-gate printf(" %.0f\n", ii);
1020Sstevel@tonic-gate nn = nn/ii;
1030Sstevel@tonic-gate vv = 1 + sqrt(nn);
1040Sstevel@tonic-gate goto retry;
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate if(nn > 1.0){
1090Sstevel@tonic-gate printf(" %.0f\n", nn);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate printf("\n");
1120Sstevel@tonic-gate }
113*239Sceastha return (0);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
116*239Sceastha void
try(double arg)117*239Sceastha try(double arg)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate double temp;
1200Sstevel@tonic-gate retry:
1210Sstevel@tonic-gate modf(nn/arg, &temp);
1220Sstevel@tonic-gate if(nn == temp*arg){
1230Sstevel@tonic-gate printf(" %.0f\n", arg);
1240Sstevel@tonic-gate nn = nn/arg;
1250Sstevel@tonic-gate vv = 1 + sqrt(nn);
1260Sstevel@tonic-gate goto retry;
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate }
129