The algorithm logic reproduces the number of the end 0 from the calculated factorial n!:
Problem Description
given the parameter n (n is a positive integer), calculate the factorial of n n! The number of "0" is included at the end.
for example, 5! =120, which contains the number of "0" at the end of the 1;10! = 3628800, with the number of "0" at the end of the 2;20! = 243290 -176640000, the number of "0" at the end contains 4.
Calculation Formula
First, the calculation formula is given, and the derivation process is given later.
So that f (x) indicates the number of "0" at the end of the positive integer x, then there are:
When 0 < n < 5 o'clock, f (n!) = 0;
When n >= 5 o'clock, f (n!) = k + F (k!), where k = N/5 (rounding).
Problem Analysis
obviously, for the large number of factorial, we can not calculate its results, and then count the number of "0" at the end. Therefore, it must be analyzed from its digital characteristics. Below we analyze from the angle of factorization.
let's consider the general situation first. For any positive integer, if it is factored, then the "0" at the end of it must be decomposed into 2*5. Here, each "0" must correspond to a factor of "5". However, please note thatThe factor "5" in the factorization of a number does not necessarily correspond to a "0", because a factor of "2" is required to achieve its one by one counterpart.
let's go back to the original question. Here is a first conclusion:
Conclusion 1: For the factorial n! of n , in its factorization, if there is a factor "5", then it must correspond to the n! A "0" at the end.
under this conclusion to prove that:
(1) when n < 5 o'clock, the conclusion is clearly established.
(2)when n >= 5 o'clock, make n! = [5k * 5 (K-1) * ... * 5] * A, where n = 5k + R (0 <= R <= 4), A is an integer with no factor "5".
for sequences 5k, 5 (k-1), ..., 10, 5 each of 5i (1 <= i <= k), all contain a factor of "5", and in the interval (5 (i-1), 5i) (1 <= i <= k)Memoryin an even number, that is, there is a factor "2" in a that corresponds to 5i. That is, here the K-factor "5" and the n! At the end of the K "0" one by one corresponds.
we further put n! Expressed as: n! = 5^k * k! * A (equation 1), where 5^k represents 5 of the K-th square. It is easy to make use of (1) and iterative methods to reach the conclusion 1.
it proves the factorial of n n! At the end of the "0" and n! The factorization of the factor "5" is one by one corresponding. In other words, the factorial n! of N is computed The number of "0" at the end can be converted to calculate the number of "5" in its factorization.
so that f (x) denotes the number of "0" at the end of the positive integer x, and g (x) indicates the number of factors "5" in the factorization of positive integer x, then using the above conclusions 1 and 1 are:
f (n!) = g (n!) = g (5^k * k! * a) = k + g (k!) = k + f (k!)
So, the final calculation formula is:
when 0 < n < 5 o'clock, f (n!) = 0;
when n >= 5 o'clock, f (n!) = k + F (k!), where k = N/5 (rounding).
Example of calculation
f (5!) = 1 + f (1!) = 1
f (10!) = 2 + f (2!) = 2
f (20!) = 4 + f (4!) = 4
f (100!) = + F (20!) = 4 + f (4!) =
f (1000!) = max + F (200!) = 8 + + f (40!) = + + F (8!) = 248 + 1 + f (1) =249
Java Code Implementation:
package com.java.algorithm.test;import org.junit.test;public class nfactzerotest { @Test public void testnfactzero () { int N=100; int Num0=get0num (N); system.out.println (N+ "! has 0 In the end: "+NUM0"); } public int get0num (int nfact) { int num0=0; if (nfact>0 && nfact<5) { num0=0; }else if (nFact>=5 ) { int times=nfact/5; num0=times+this.get0num (Times); } return num0; }}
This article is from the "Tranquility Zhiyuan" blog, please be sure to keep this source http://woodpecker.blog.51cto.com/2349932/1955915
Algorithm-Calculates the number of factorial n! at the end of 0