i needed to do the -lm to get the object file which has sqrt() defined linked in - per a google search. anywho - here is my response, kinda sucky because there are redundant #'s and little error checking and i am sure it could be done with fewer lines.
it under debian and redhat it compiles fine with
gcc -lm -o numfact numfact.c
Code:#include<stdio.h> #include<math.h> /* okay lets find all pairs of factors for an input number * all input numbers must be integers greater than zero * and checked to see if it is valid. Number must also be * less than the maximum value for a unsigned interger (16 bits) */ #define TRUE 1 #define FALSE 0 #define START 1 int is_valid(int num); /* determines if passed value is okay */ void factor_num(int num); /* factors the numbers */ int main() { int number; /* get value from user */ puts("enter an interger greater than zero to get its factors \n"); scanf("%d",&number); if (is_valid(number)==FALSE) { exit(0); } factor_num(number); return(0); } /* lets see if we were passed garbage - this will return * FALSE if the input is not valid and TRUE if the input * is valid */ int is_valid(int num) { if (num > 65536) { puts("that number is too fucking big \n"); return(FALSE); } else if (num <= 0) { puts("either thats zero - or less than zero \n"); return(FALSE); } else { return(TRUE); } } /* this will find all pairs of numbers which are factors of the * number passed in and print them. */ void factor_num(number) { int *val1; int *val2; int index = START; /*start at 1, number and 1 will always be factors */ int total_factor_pairs = 1; /* there will always be one pair*/ int i; float lowlim,highlim; val1= (int*)malloc(256*sizeof(int)); val2= (int*)malloc(256*sizeof(int)); if(number <= 3) { lowlim = 1; } else { lowlim = sqrt(number) ; } highlim = number/2; *val1 = number; *val2 = 1; /*only need from the sqrt of num ber to 1/2 number */ for ( i = lowlim; i <= highlim ; i++) { if(number%i == 0) { total_factor_pairs++; *(val1+index) = number/i; *(val2+index) = i; index++; } } puts(" "); printf("there are %d factor pairs, they are:\n",total_factor_pairs); for(i = total_factor_pairs-1 ;i >= 0 ;i--) { printf("\t %d \t %d \n",*(val1+i),*(val2+i)); } }


Reply With Quote
Bookmarks