Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
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);
}