1024! How many zeros are there at the end?

Source: Internet
Author: User

I have seen this question several times before, and I don't remember where it was.

1024 can be counted as N

There are two solutions:

I. Using Big integer operations is also a "brute force solution. However, the length of the possible solution is the next hop. This method is not advisable.

2. It is not difficult to find out using mathematical theories:

1. The number of result 0 is irrelevant to the number smaller than 5 in N. Not a multiple of 5, no matter which one is multiplied, the result of a single digit being 0 is impossible.

The number of even numbers within n must be greater than 5 (5 and even numbers can be multiplied to increase the number of last 0 ).

So there are a = N/5;

2. Let's take a look at 25. Is this special? 25*4 = 100, two zeros can be generated. Why? Because 25 = 5*5, the number of excess zeros that can be generated is related to the number of contained factors 5.

25*4 = 100, there are two 0,100, no matter the number is multiplied by any number that is not a factor of 10, it is impossible to generate an extra number of two 0. Likewise (75 = 25*3 = 5*5*3) * 4 = 300; (125 = 5*5*5) * 8 = 1000;

That is, the number of factors 5 in a number can be multiplied by enough even numbers to generate the last 0 of the same number.

Summary: The number of the last 0 in the factorial of N is: N/5 + N/25 + N/125 ......

Computing TestProgram:

# Include <stdio. h> int zero_count (int n); int main (INT argc, char * argv []) {printf ("zero_count is % d... \ n ", zero_count (1024); Return 0;} int zero_count (int n) {int COUNT = 0; while (n> 0) {n = N/5; count + = N;} return count ;}

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.