c - How does this code find the number of trailing zeros from any base number factorial? -
The code below works perfectly, but I will explain to anyone some math behind it. Actually, how does it work?
  #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; (int, arg arg, char * argv []) {const int base = (x, y)) ((x ()) and (y)) (x): (y)) (x): (y) 16 ; Int n, i, j, p, c, noz, k; N = 7; / * 7! = Decimal 5040 or 0x13 b - zero behind zero = / noz = n; J = base; / * Why do we start from 2 * / i (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 math behind this loop? * / While (k / i & gt; 0) {c + = k / i; k / = i; } Nose = Min (Nose, C / P); }} Printf ("% d!% D trailing zeros \ n", n, noz); Return 0; }     
  Note that the problem is equivalent to finding the highest power of the base   Which  n! . If the base was dominant (let's call it  p ), then we can use it to compute the highest  p  the power  n! :    
  
 Let us remove the part of our code that is in the function:  
  int max_power_of_p_in_fac (int p, int n) {int mu = 0; While (n / p & gt; 0) {mu + = n / p; n / = p; } Return mu; }    What will happen if  base  is a prime power? Suppose we have  base = p  q   then  μ   p  has the highest power of  n!  and  r = the floor (μ / q) divides , we have    (p ^ q) ^ r = p ^ (qr) div ^ P ^ μ divides n!   
 and  
  (p ^ q) ^ (r + 1) = p ^ (q (r + 1)) gt; = p ^ (μ + 1)  not  divide n    then  r  in N is the maximum power of  p ^ q ! Let's also write the function for this:    int max_power_of_pq_in_fac (int p, int q, int n) {return max_power_of_p_in_fac (p, n) / q; }    What if the  base  is the normal number? Assume that    base = page  1   1   p  2  <    > 
 (This is the unique experimental factor of  base ) after that all of us p  i   q  i   Solve the problem and take the minimum of them:    Int max_power_of_base_in_fac (int base, int n) {int res = infinity; For every factor p ^ q in the original factor: res = min (res, max_power_of_pq_in_fac (p, q, n)); Withdrawal reserve; }     base  how to subtract? Well we can just use the test division, as your example code, we start by checking whether 2 is a major factor or not. If so, then we calculate  maximum_power_of_pq_in_fac  and divide the  base  until it is not divisible by 2. Then we proceed with the next candidate factor:    zero factorize (int base) {for (int p = 2; p & lt; = base; ++ p) {if (base % p == 0) {// If the base is divisible by p, p is a key factor int q = 0; While (base% P == 0) {// calculation q and rid all the p factors q ++; Base / = p; } // Do something with the factor ^ q} / proceed with the next candidate developer}}    By carefully examining the code, you will find that all of the above elements are included, only Put together in a loop, which is a bit confusing.  
  Update:  If you are interested, then complexity  O (base * log n)  in the algorithm presented by you. You can easily make it  o (sqrt (base) * log n)  By making the regularity of the prime factor slightly favorable:    zero element (int base) Check for similar primary factors until {for (int p = 2; p * p & lt; = base; ++ p) {// only sqrt (base) // ... as before) if (base) {/ / There is absolutely a prime factor & gt; Sqrt (base). // This is definitely the plurality of 1. // Process Prime factor Base ^ 1}} And of course you can use any other more sophisticated centralization algorithm if you want to speed things up more.   
 
Comments
Post a Comment