17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
22*0d8b5334Sceastha /*
23*0d8b5334Sceastha * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24*0d8b5334Sceastha * Use is subject to license terms.
25*0d8b5334Sceastha */
26*0d8b5334Sceastha
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /* factor COMPILE: cc -O factor.c -s -i -lm -o factor */
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate * works up to 14 digit numbers
337c478bd9Sstevel@tonic-gate * running time is proportional to sqrt(n)
347c478bd9Sstevel@tonic-gate * accepts arguments either as input or on command line
357c478bd9Sstevel@tonic-gate * 0 input terminates processing
367c478bd9Sstevel@tonic-gate */
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate double modf(), sqrt();
397c478bd9Sstevel@tonic-gate double nn, vv;
407c478bd9Sstevel@tonic-gate double huge = 1.0e14;
417c478bd9Sstevel@tonic-gate double sq[] = {
427c478bd9Sstevel@tonic-gate 10, 2, 4, 2, 4, 6, 2, 6,
437c478bd9Sstevel@tonic-gate 4, 2, 4, 6, 6, 2, 6, 4,
447c478bd9Sstevel@tonic-gate 2, 6, 4, 6, 8, 4, 2, 4,
457c478bd9Sstevel@tonic-gate 2, 4, 8, 6, 4, 6, 2, 4,
467c478bd9Sstevel@tonic-gate 6, 2, 6, 6, 4, 2, 4, 6,
477c478bd9Sstevel@tonic-gate 2, 6, 4, 2, 4, 2,10, 2,
487c478bd9Sstevel@tonic-gate };
497c478bd9Sstevel@tonic-gate
50*0d8b5334Sceastha void try(double);
51*0d8b5334Sceastha
52*0d8b5334Sceastha int
main(int argc,char * argv[])53*0d8b5334Sceastha main(int argc, char *argv[])
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate int test = 1;
567c478bd9Sstevel@tonic-gate int ret;
57*0d8b5334Sceastha int j;
587c478bd9Sstevel@tonic-gate double junk, temp;
597c478bd9Sstevel@tonic-gate double fr;
607c478bd9Sstevel@tonic-gate double ii;
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate if(argc > 2){
637c478bd9Sstevel@tonic-gate printf("Usage: factor number\n");
647c478bd9Sstevel@tonic-gate exit(1);
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate if(argc == 2){
677c478bd9Sstevel@tonic-gate ret = sscanf(argv[1], "%lf", &nn);
687c478bd9Sstevel@tonic-gate test = 0;
697c478bd9Sstevel@tonic-gate printf("%.0f\n", nn);
707c478bd9Sstevel@tonic-gate goto start;
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate while(test == 1){
737c478bd9Sstevel@tonic-gate ret = scanf("%lf", &nn);
747c478bd9Sstevel@tonic-gate start:
757c478bd9Sstevel@tonic-gate if((ret<1) || (nn == 0.0)){
767c478bd9Sstevel@tonic-gate exit(0);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate if((nn<0.0) || (nn>huge)){
797c478bd9Sstevel@tonic-gate printf("Ouch!\n");
807c478bd9Sstevel@tonic-gate continue;
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate fr = modf(nn, &junk);
837c478bd9Sstevel@tonic-gate if(fr != 0.0){
847c478bd9Sstevel@tonic-gate printf("Not an integer!\n");
857c478bd9Sstevel@tonic-gate continue;
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate vv = 1. + sqrt(nn);
887c478bd9Sstevel@tonic-gate try(2.0);
897c478bd9Sstevel@tonic-gate try(3.0);
907c478bd9Sstevel@tonic-gate try(5.0);
917c478bd9Sstevel@tonic-gate try(7.0);
927c478bd9Sstevel@tonic-gate ii = 1.0;
937c478bd9Sstevel@tonic-gate while(ii <= vv){
947c478bd9Sstevel@tonic-gate for(j=0; j<48; j++){
957c478bd9Sstevel@tonic-gate ii += sq[j];
967c478bd9Sstevel@tonic-gate retry:
977c478bd9Sstevel@tonic-gate modf(nn/ii, &temp);
987c478bd9Sstevel@tonic-gate if(nn == temp*ii){
997c478bd9Sstevel@tonic-gate printf(" %.0f\n", ii);
1007c478bd9Sstevel@tonic-gate nn = nn/ii;
1017c478bd9Sstevel@tonic-gate vv = 1 + sqrt(nn);
1027c478bd9Sstevel@tonic-gate goto retry;
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate if(nn > 1.0){
1077c478bd9Sstevel@tonic-gate printf(" %.0f\n", nn);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate printf("\n");
1107c478bd9Sstevel@tonic-gate }
111*0d8b5334Sceastha return (0);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate
114*0d8b5334Sceastha void
try(double arg)115*0d8b5334Sceastha try(double arg)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate double temp;
1187c478bd9Sstevel@tonic-gate retry:
1197c478bd9Sstevel@tonic-gate modf(nn/arg, &temp);
1207c478bd9Sstevel@tonic-gate if(nn == temp*arg){
1217c478bd9Sstevel@tonic-gate printf(" %.0f\n", arg);
1227c478bd9Sstevel@tonic-gate nn = nn/arg;
1237c478bd9Sstevel@tonic-gate vv = 1 + sqrt(nn);
1247c478bd9Sstevel@tonic-gate goto retry;
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate }
127