I’m doing this problem: http://www.codechef.com/problems/FCTRL
I have the solution, but, the memory usage is coming out to be 1.6 MB, which, apparently, is too much. I don’t understand how I can decrease this seeing that I have almost no persistent data. Here’s my code:
#include <stdio.h>
#include <math.h>
int maxPower(long x) {
int i;
for(i = 0; i<= 100; i++) {
long myPower = pow(5,i);
if(myPower > x) {
return (i-1);
}
}
}
int main (void) {
int lines;
scanf("%d", &lines);
int i;
for(i = 0; i<lines; i++) {
long temp;
scanf("%ld", &temp);
int five_counter = 0;
int myPower = maxPower(temp);
int power;
for(power = 1; power<=myPower; power++) {
five_counter += floor(temp/((int)(pow(5,power))));
}
printf("%d\n", five_counter);
five_counter = 0;
}
}
As you can see, its written in C. Any ideas on how to decrease memory usage?
This is what i submitted and it was accepted
The main logic is that no of zeroes at the end of a factorial is the highest power of 5 that divides the number.