CodeChef Easy Problem: Factorial


Facebook Hacker Cup 2013 is coming up, so I started solving some problems on CodeChef.com to warm up. Here’s one of them:

——————–

The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceivers form the areas called cells (this term gave the name to the cellular phone) and every phone connects to the BTS with the strongest signal (in a little simplified view). Of course, BTSes need some attention and technicians need to check their function periodically.

The technicians faced a very interesting problem recently. Given a set of BTSes to visit, they needed to find the shortest path to visit all of the given points and return back to the central company building. Programmers have spent several months studying this problem but with no results. They were unable to find the solution fast enough. After a long time, one of the programmers found this problem in a conference article. Unfortunately, he found that the problem is so called “Traveling Salesman Problem” and it is very hard to solve. If we have N BTSes to be visited, we can visit them in
any order, giving us N! possibilities to examine. The function expressing that number is called factorial and can be computed as a product

1.2.3.4….N. The number is very high even for a relatively small N.

The programmers understood they had no chance to solve the problem. But because they have already received the research grant from the government, they needed to continue with their studies and produce at least some results. So they started to study behavior of the factorial function.

For example, they defined the function Z. For any positive integer N, Z(N) is the number of zeros at the end of the decimal form of number N!. They noticed that this function never decreases. If we have two numbers N1Input
There is a single positive integer T on the first line of input (equal to about 100000). It stands for the number of numbers to follow. Then there are T lines, each containing exactly one positive integer number N, 1 <= N <= 1000000000. Output
For every number N, output a single line containing the single non-negative integer Z(N).

Sample Input:
6
3
60
100
1024
23456
8735373
Sample Output:
0
14
24
253
5861
2183837

——————–

My Solution

As usual the first attempt was a brute force algorithm. I simply created an algorithm to keep multiplying the numbers, and at every iteration I checked if the rightmost digits were zero, and if so increased a counter and removed them from the number. I also kept using % 100000 to keep the number small, else it would quickly go out of range, even using unsigned long long. Here’s the C program:

#include <stdio.h>

unsigned long long z (unsigned long long x){  
  unsigned long long result = 0;
  unsigned long long i,digit;

  digit = 1;
  
  for (i=1;i<=x;i++){    
    digit *= i;    
    while(digit!=0&&digit%10==0){      
      result++;
      digit/=10;      
    }    
    digit = digit % 100000;
      
  }

  return result;  
  
}

int main(){
  int i,j;
  scanf("%d",&j);
  unsigned long long x;

  for (i=0;i<j;i++){
    scanf("%llu",&x);    
    printf("%llun",z(x));  
  }  

return 0;
}

Given that the level of this problem is “easy” I figured the brute force approach would work, but it didn’t. The program was too slow.

After that I looked at the growth pattern of zeroes in factorial numbers, and discovered that the number grows every 5 multiplications, and that at the fifth consecutive increment it jumps by 2 instead of 1. I played around a bit and concluded that in order to find the number of zeroes of a certain number (in factorial form) all you have to do is to keep dividing it by 5 until you can, summing up the partial results. Here’s the algorithm which solved the problem correctly:

#include <stdio.h>

unsigned long long z (unsigned long long x){
  unsigned long long result = 0;
  unsigned long long i;
  
  while(x>0){
    x /= 5;
    result += x;
  }

  return result;  
  
}

int main(){
  int i,j;
  scanf("%d",&j);
  unsigned long long x;

  for (i=0;i<j;i++){
    scanf("%llu",&x);    
    printf("%llun",z(x));  
  }  

return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *