Back to list of problems
Factors and Factorials
160.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> int primes[]={ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 }; #define NUM_PRIMES (sizeof(primes)/sizeof(int)) int vector[NUM_PRIMES]; void factoriza(int num) { int i; for(i=0; i<NUM_PRIMES; i++) { while ((num%(primes[i]))==0) { vector[i]++; num /= primes[i]; } } } void factorial(int i) { if (i==1) { return; } factoriza(i); factorial(i-1); } int main(void) { char buf[1024]; int i,j,k; while(fgets(buf, 1024, stdin)) { if (sscanf(buf, "%d", &i)!=1) { exit(1); } if (i==0) { exit(0); } for(j=0; j<NUM_PRIMES; j++) { vector[j]=0; } factorial(i); printf("%3d! =", i); k=0; for(j=0; j<NUM_PRIMES; j++) { if (vector[j]) { k=j; } } for(j=0; j<=k; j++) { if (j && ((j%15)==0)) { printf("\n "); } printf("%3d", vector[j]); } printf("\n"); } exit(0); }