N! Number of consecutive zeros at the end

Source: Internet
Author: User

Baidu and EMC pen title: For any input positive integer N, write a C program to calculate n! The number of consecutive zeros at the end of, and the computing complexity is pointed out. Example: 18! = 6402373705728000, the number of consecutive 0 tails is 3. (You do not need to consider the problem that the value exceeds the integer limit of the computer)

Train of Thought Analysis:

This question uses a mathematical method to solve the problem with the highest efficiency. If K is 0 in a row, it indicates that it is a multiple of 10 ^ K, that is (2 × 5) ^ K = 2 ^ K * 5 ^ K; the number to be calculated is N * (N-1) (N-2 )......... 1. Since each two numbers can at least break down one, 2 is definitely more than five, so the number of K depends on the above decomposition factor, there are several 5 problems; 5 is only a multiple of 5, and 5 is a multiple of 5.

//////////////////////////////////////// ////////////

Method 1:

Int zerosforn_1 (int n)

{

Int fives, result = 0, I;

For (fives = 5; n> = fives; fives + = 5) // The number of cycles is N/5

{

For (I = fives; I % 5 = 0; I/= 5) // The maximum number of loops here is log5 (N)

{

++ Result;

}

}

// Printf ("% d/N", result );

Return result;

}

The complexity of the for loop algorithm is the easiest to see, that is, the number of for loops. the maximum number of loops is N/5 * log5 (N). Therefore, the complexity is O (nlogn ).

The idea is the clearest, that is, for each value in multiples of 5, you can find the number of factors and

//////////////////////////////////////// ////////////

Method 2:

Int zerosforn_2 (int n)

{

Int pow5, result = 0;

For (pow5 = 5; n> = pow5; pow5 * = 5) // The number of loops here is log5 (N)

{

Result + = N/pow5;

}

// Printf ("% d/N", result );

Return result;

}

N remains the same, and pow5 increments by the power of 5. The idea of this algorithm is to find the number of all vertices divided by 5 within n, all the numbers divided by 25 (A 5 factor is added on the basis of 5), and all the numbers divided by 125 (a 5 factor is added on the basis of 25 )....

Set the maximum number to N,
Set 5 ^ (n + 1)> N> = 5 ^ n
[N/5] + [N/(5 ^ 2)] + [N/(5 ^ 3)] +... + [N/(5 ^ n)] indicates the number of consecutive 0 values.

The number of sub-items in the preceding formula is log5 (N), that is, the number of cycles. Therefore, the complexity is log5 (n)

//////////////////////////////////////// ////////////

Method 3:

[N/5] + [N/(5 ^ 2)] + [N/(5 ^ 3)] +... + [N/(5 ^ n)]

= [N/5] + [[N/5]/5] + [[N/5]/5]/5] +... + [...]

= A1 + [A1/5] + [A2/5] +... + [An-1/5]

That is, the above components constitute an proportional series, an = An-1/5, the proportional ratio is 1/5

That is, if A1 is divided by 5 repeatedly, as long as it is greater than 0, that is, the addition, the following algorithm is obtained:

Int zerosforn_3 (int n)

{

Int result = 0;

N/= 5; // A1

While (n> 0)

{

Result + = N; // sum

N/= 5; // calculate

}

// Printf ("% d/N", result );

Return result;

}

The number of items in the proportional series is log5 (N), that is, the number of cycles. Therefore, the complexity is log5 (n)

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.

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.