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