Number of 0 of the mantissa of the integer factorial result

Source: Internet
Author: User

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! = 2432902008176640000, with the number of "0" at the end of which is 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". Note, however, that the 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.
The following is a proof of this conclusion:
(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 sequence 5k, 5 (k-1), ..., 10, 5 in each number 5i (1 <= i <= k), there is a factor "5", and in the interval (5 (i-1), 5i) (1 <= i <= K) there are even, that is, a factor "2" and 5i Correspond. 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!) = 24
F (1000!) = max + F (200!) = 8 + + f (40!) = + + F (8!) = 248 + 1 + f (1) =249

Number of 0 of the mantissa of the integer factorial result

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.