"Algorithmic supplements" factorial

Source: Internet
Author: User

Reprint Please specify the Source: http://blog.csdn.net/ns_code/article/details/28335353

Preface

Mainly look at two questions about factorial, from which we can see some laws.

Topic one

N! Number of end 0

Looking at the number of 0 occurrences, we are going to find the multiplier that produces 0, that is, which number is multiplied to get 10. We need to be on N. Decomposition of the mass factor. Because 10 = 2*5, so the number of 0 is as far as n! The logarithm of the occurrence of 2 and 5 is related. And the number that can be divisible by 2 is much more than the number that can be divisible by 5. So we find n! The number of medium-quality factor 5 appears, that is, n! The number of the end 0.

method One

The most straightforward approach. is to calculate the number of 5 in the factorization of each number in the 1-n, and then Add. The code is as follows:

int factorialnum0_1 (int n) {int count = 0;int i;for (i=1;i<=n;i++) {int J = i;while (j%5 = = 0) {count++;j/= 5;}} return count;}
The time complexity of such a method is O (n).

method Two

In method one, the number without factorization 5 is also inferred. The complexity of the time is high.

Another method is to use the following conclusions, for example:

N. The number of mass factor k contained in: [n/k]+[n/k^2]+[n/k^3]+ ... (There will always be a T, making k^t>n, there is [n/k^t]=0)

The proof of this formula is not difficult, and I try to understand it, [n/k] equals 1. 2,3 ... The number of numbers in n that can be divisible by K.

The code written in this method is as follows;

int factorialnum0_2 (int n) {int count = 0;while (n) {count + = N/5;n/= 5;} return count;}
The time complexity of the method is O (log5n) (here is the logarithm of base N of 5).


Topic two

N!

The position of the lowest bit 1 in the binary representation

For example 3! is 6, and the binary is 1010. So the lowest bit of the 1 position is 2, where the leftmost position is calculated from the start of 1.

method One

We if the lowest bit of 1 of the position is the K-bit. Then n! =2^ (k-1) +...+2^t+...+2^p+ ..., here t,p and so on means the t+1 bit behind. The first p+1 is also 1, and p>t>k. So very easy to see out, N! The number of medium-mass factor 2 is k-1, that is. The position of the lowest bit 1 is n!

The number of medium-quality factor 2 plus 1, so the problem is converted to n! The number of medium-quality factor 2, the same use of the conclusion:

N. The number of mass factor k contained in: [n/k]+[n/k^2]+[n/k^3]+ ... (There will always be a T, making k^t>n, there is [n/k^t]=0)

The code written in this way is as follows:

int Lowest1 (int n) {int count = 0;while (n) {count + = N/2;n/= 2;} return count + 1;}
The method has a time complexity of O (log2n) (here is the logarithm of the base N of 2)。

method Two

Use for example the following conclusion: N!

Contains the number of factorization 2. equals n minus N for the number of 1 in the binary representation. about how to find the number of binary representation of N 1, take my blog post; http://blog.csdn.net/ns_code/article/details/25425577, the quickest way to do this is just the same as the number of 1 in the binary, So the time complexity of such a method is better. Although method one is already very fast. The time complexity of such a method is O (k), where K is n. The number of 1.

The code for such a method is no longer given.


"Algorithmic supplements" factorial

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.