/* ** primefactor.c: produce the prime factorization of a given number ** partial solution to hw#8 ** -- written by Jeff Tian, 3/2/99. */ #include #include #include #include "genlib.h" #include "simpio.h" void PrimeFactor(int x); main() { int n; char c='y'; printf("This program produces prime factorization for input numbers.\n\n"); while (c == 'y' || c == 'Y') { printf("Please enter a positive integer: "); n = GetInteger(); while (n <= 0 ) { printf("You need to enter a positive integer. Try again: "); n = GetInteger(); } PrimeFactor(n); printf("\nWould you like to try another number [y/n] ? "); while (isspace(c = getchar())); while (getchar() != '\n'); } } /* ** PrimeFactor() -- an efficient function for prime factorization */ void PrimeFactor(int x) { int i=2, limit; printf("The prime factors for %d is:\n", x); limit = sqrt(x); while (i <= limit ) { if(x%i == 0) { printf("%d X ", i); x = x/i; limit = sqrt(x); } else { i++; } } printf("%d\n", x); }