How many zeros are there behind the factorial result of n? such as the factorial of 2016 (NetEase's written selection questions)

Source: Internet
Author: User

The original address of the article: http://blog.csdn.net/zyh2525246/article/details/53697136

When the order multiplier is small, it can be calculated directly.

For example: Ask for 10. The number of the following 0.   The result is obviously 3628800. The number of 0 is 2.

20. The result is a number of 4 2432902008176640000,0. And this time has reached the 19-digit number.

A larger number, if you go straight to the request. It's obviously too much trouble.

Second, the law behind

This paper introduces a method to find the number of 0 after a certain number factorial result.

First, zero occurs when multiples of 5 are multiplied by even numbers.

But the number that appears 0 is not necessarily, 2*5=10 appears a 0. 4*25=100 will appear 2 0. 8*125=1000 appears 3 0.

It is easy to find the rules. 4*25 can be decomposed into 2*5*2*5,8*125 which can be decomposed into 2*5*2*5*2*5 ...

As for the 10,15,20 these numbers. Decomposition, only a 5 will appear, will only appear 1 0.

As for 50,75 these multiples of 25, decomposition, will only appear 2 5, and even multiplication will only appear 2 0.

Here is the count of 0.

As mentioned above, even times 5 will appear a 0, multiplied by 5 squared will appear 2 0, multiplied by 5 of the Cubic will appear 3 0.

So how do you calculate how many multiples of 5 are present in factorial, multiples of 25, and multiples of 125? Also, there is a problem to note that 25,125 is also a multiple of 5, 125 is also a multiple of 25.

For example, ask for 100. The number of the following 0.

The method mentioned above is that the number of multiples of 5 is 16 (minus the multiples of 25), the number of multiples of 25 is 4, and the result should be 16*1+4*2=24 of 0.

We can also change a thought, appear a 5 is a 0, then appear a 25 is 2 0, if the multiples of 5 does not exclude the multiples of 25, each appears a multiple of 25, only add a 0 can.

In other words, the number of 0 is equal to the number of multiples of 5 plus the number of multiples of 25, that is, 20+4.

If it is to ask 2016. The number of the back 0.

Also follow the above method.

The number of multiples of 5 is: 2016/5 = 403

The number of multiples of 25 is: 403/5 = 80

The number of multiples of 125 is: 80/5 = 16

The number of multiples of 625 is: 16/5 = 3.

So we can draw 2016. The number of the following 0 is: 403+80+16+3 = 502.

How to use the code and how to implement it.

Very simple, with a simple loop can be solved, of course, recursive algorithm is better. Here we just paste the loop code.

Scanner Scanner = new Scanner (system.in);
System.out.print ("Please enter the Order multiplier:");
int a = Scanner.nextint ();
int count = 0;
for (;;) {
A = A/5;
if (a = = 0) {
Break
}else {
count+= A;
}
}
System.out.println ("The number of 0 after the factorial result is:" +count);

The results are as follows:

Please enter the Order multiplier: 2016
The number of 0 after the factorial result is: 502

Of course, the weight in the mind, the code has nothing to say. I hope you have learned this method. Although it is of little use in life, the method of transforming ideas is very useful.

Related Article

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.

Tags Index: