title: How does this code find the number of trailing zeros from any base number factorial?
body: <p>The code below works perfectly but I would like someone to explain to me the mathematics behind it.  Basically, how does it work?</p>
  
  <pre><code>#include &lt;stdio.h&gt; 
  #include &lt;stdlib.h&gt;  /* atoi */
  
  #define min(x, y) (((x) &lt; (y)) ? (x) : (y))
  
  int main(int argc, char* argv[])
  {
     const int base = 16;
     int n,i,j,p,c,noz,k;
  
     n = 7;  /* 7! = decimal 5040 or 0x13B0  - 1 trailing zero */  
     noz = n;
     j = base;
     /* Why do we start from 2 */
     for (i=2; i &lt;= base; i++)
     {
        if (j % i == 0)
        {   
           p = 0;  /* What is p? */
           while (j % i== 0)
           {
              p++;
              j /= i;
           }
           c = 0;
           k = n;
           /* What is the maths behind this while loop? */
           while (k/i &gt; 0)
           {
              c += k/i;
              k /= i;
           }
           noz = min(noz, c/p);
        }
     }
     printf("%d! has %d trailing zeros\n", n, noz);
  
     return 0;
  }
  </code></pre>