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 30*0d8b5334Sceastha #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate /* factor COMPILE: cc -O factor.c -s -i -lm -o factor */ 337c478bd9Sstevel@tonic-gate /* 347c478bd9Sstevel@tonic-gate * works up to 14 digit numbers 357c478bd9Sstevel@tonic-gate * running time is proportional to sqrt(n) 367c478bd9Sstevel@tonic-gate * accepts arguments either as input or on command line 377c478bd9Sstevel@tonic-gate * 0 input terminates processing 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate double modf(), sqrt(); 417c478bd9Sstevel@tonic-gate double nn, vv; 427c478bd9Sstevel@tonic-gate double huge = 1.0e14; 437c478bd9Sstevel@tonic-gate double sq[] = { 447c478bd9Sstevel@tonic-gate 10, 2, 4, 2, 4, 6, 2, 6, 457c478bd9Sstevel@tonic-gate 4, 2, 4, 6, 6, 2, 6, 4, 467c478bd9Sstevel@tonic-gate 2, 6, 4, 6, 8, 4, 2, 4, 477c478bd9Sstevel@tonic-gate 2, 4, 8, 6, 4, 6, 2, 4, 487c478bd9Sstevel@tonic-gate 6, 2, 6, 6, 4, 2, 4, 6, 497c478bd9Sstevel@tonic-gate 2, 6, 4, 2, 4, 2,10, 2, 507c478bd9Sstevel@tonic-gate }; 517c478bd9Sstevel@tonic-gate 52*0d8b5334Sceastha void try(double); 53*0d8b5334Sceastha 54*0d8b5334Sceastha int 55*0d8b5334Sceastha main(int argc, char *argv[]) 567c478bd9Sstevel@tonic-gate { 577c478bd9Sstevel@tonic-gate int test = 1; 587c478bd9Sstevel@tonic-gate int ret; 59*0d8b5334Sceastha int j; 607c478bd9Sstevel@tonic-gate double junk, temp; 617c478bd9Sstevel@tonic-gate double fr; 627c478bd9Sstevel@tonic-gate double ii; 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate if(argc > 2){ 657c478bd9Sstevel@tonic-gate printf("Usage: factor number\n"); 667c478bd9Sstevel@tonic-gate exit(1); 677c478bd9Sstevel@tonic-gate } 687c478bd9Sstevel@tonic-gate if(argc == 2){ 697c478bd9Sstevel@tonic-gate ret = sscanf(argv[1], "%lf", &nn); 707c478bd9Sstevel@tonic-gate test = 0; 717c478bd9Sstevel@tonic-gate printf("%.0f\n", nn); 727c478bd9Sstevel@tonic-gate goto start; 737c478bd9Sstevel@tonic-gate } 747c478bd9Sstevel@tonic-gate while(test == 1){ 757c478bd9Sstevel@tonic-gate ret = scanf("%lf", &nn); 767c478bd9Sstevel@tonic-gate start: 777c478bd9Sstevel@tonic-gate if((ret<1) || (nn == 0.0)){ 787c478bd9Sstevel@tonic-gate exit(0); 797c478bd9Sstevel@tonic-gate } 807c478bd9Sstevel@tonic-gate if((nn<0.0) || (nn>huge)){ 817c478bd9Sstevel@tonic-gate printf("Ouch!\n"); 827c478bd9Sstevel@tonic-gate continue; 837c478bd9Sstevel@tonic-gate } 847c478bd9Sstevel@tonic-gate fr = modf(nn, &junk); 857c478bd9Sstevel@tonic-gate if(fr != 0.0){ 867c478bd9Sstevel@tonic-gate printf("Not an integer!\n"); 877c478bd9Sstevel@tonic-gate continue; 887c478bd9Sstevel@tonic-gate } 897c478bd9Sstevel@tonic-gate vv = 1. + sqrt(nn); 907c478bd9Sstevel@tonic-gate try(2.0); 917c478bd9Sstevel@tonic-gate try(3.0); 927c478bd9Sstevel@tonic-gate try(5.0); 937c478bd9Sstevel@tonic-gate try(7.0); 947c478bd9Sstevel@tonic-gate ii = 1.0; 957c478bd9Sstevel@tonic-gate while(ii <= vv){ 967c478bd9Sstevel@tonic-gate for(j=0; j<48; j++){ 977c478bd9Sstevel@tonic-gate ii += sq[j]; 987c478bd9Sstevel@tonic-gate retry: 997c478bd9Sstevel@tonic-gate modf(nn/ii, &temp); 1007c478bd9Sstevel@tonic-gate if(nn == temp*ii){ 1017c478bd9Sstevel@tonic-gate printf(" %.0f\n", ii); 1027c478bd9Sstevel@tonic-gate nn = nn/ii; 1037c478bd9Sstevel@tonic-gate vv = 1 + sqrt(nn); 1047c478bd9Sstevel@tonic-gate goto retry; 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate if(nn > 1.0){ 1097c478bd9Sstevel@tonic-gate printf(" %.0f\n", nn); 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate printf("\n"); 1127c478bd9Sstevel@tonic-gate } 113*0d8b5334Sceastha return (0); 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 116*0d8b5334Sceastha void 117*0d8b5334Sceastha try(double arg) 1187c478bd9Sstevel@tonic-gate { 1197c478bd9Sstevel@tonic-gate double temp; 1207c478bd9Sstevel@tonic-gate retry: 1217c478bd9Sstevel@tonic-gate modf(nn/arg, &temp); 1227c478bd9Sstevel@tonic-gate if(nn == temp*arg){ 1237c478bd9Sstevel@tonic-gate printf(" %.0f\n", arg); 1247c478bd9Sstevel@tonic-gate nn = nn/arg; 1257c478bd9Sstevel@tonic-gate vv = 1 + sqrt(nn); 1267c478bd9Sstevel@tonic-gate goto retry; 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate } 129