Don't be intimidated by factorial _c language

Source: Internet
Author: User
Factorial (factorial) is a very interesting function, but many people are afraid of it, let's take a look at two factorial-related problems:
1, given an integer n, then n's factorial n! How many 0 at the end? For example: n=10,n! =3 628 800,n! Has two 0 at the end.
2 , please n! The position of the lowest bit 1 in the binary representation.
Some people encounter such a topic will think: is not to complete the calculation of n! The value? What if it overflows? In fact, if we consider the "which number multiplies to get 10" perspective, the problem becomes simple.
First consider, if n! = Kx10^m, and K cannot be divisible by 10, then n! There is an M 0 at the end. Think again about n! To decompose the mass factor, N! = (2^x) x (3^y) x (5^z) ..., because of a = 2x5, so m is only associated with X and Z, each pair of 2 and 5 can be multiplied to get a 10, so m = min (x, z). It is easy to see that x is greater than or equal to Z, because the number that can be divisible by 2 is much higher than the number divisible by 5, so the formula is reduced to M = Z.
According to the above analysis, as long as the calculation of the value of Z, you can get n! The number of the end 0.
"solution One of question 1"
The most straightforward way to compute Z is to compute the exponent of 5 in the factorization of I (I =1, 2, ..., N), and then sum:
Copy Code code as follows:

ret = 0;
for (i = 1; I <= N; i++)
{
j = i;
while (j% 5 ==0)
{
ret++; Count the number of factors in the factorial of n that can be divisible by 5
J/= 5;
}
}

"solution Two of question 1"
Formula: Z = [N/5] +[n/5^2] +[n/5^3] + ... (Do not worry that this will be an infinite operation, because there is always a K, making 5^k > N,[n/5^k]=0.) )
In a formula, [N/5] represents a multiple contribution of 5 in a number that is not greater than N, and a 5,[n/5^2] represents a multiple of 5^2 in a number that is not greater than N, and contributes another 5. The code is as follows:
Copy Code code as follows:

ret = 0;
while (N)
{
RET + N/5;
N/= 5;
}

Question 2 asks for n! The position of the lowest bit 1 in the binary representation. Given an integer n, ask N! What is the minimum digit of the binary representation 1 in the first few? For example: given N = 3,n! = 6, then n! The binary representation (1 010) of the lowest bit 1 in the second position.
In order to get a better solution, first of all to the topic of transformation.
Let's start by looking at a binary number divided by 2 of the calculation process and the results.
By dividing a binary number by 2, the actual process is as follows:
To determine whether the last bits is 0, or 0, then move the binary right one bit, which is the quotient value (why), and conversely, if 1, the binary number is odd and cannot be divisible by 2 (this is why).
So, the problem is actually equal to the n! Contains the number of decomposition 2 of +1. The answer is equal to n! Contains the number of decomposition 2 plus 1. actually n! are even, because there is a 2 in the decomposition, except 1, because the factorial of 1 is 1, it is an odd number, and the factorial of the other numbers is even.
"solution One of question 2"
Because n! Contains the number of decomposition 2, equal to N/2 + N/4 + n/8 + n/16 + ... [1],
According to the above analysis, the concrete algorithm is obtained, as follows:
Copy Code code as follows:

/*
You can first find out the number of 2 in the n! (since there is a 2 for each, then the number of
A minimum digit of one 0 more). So the position of the lowest bit of 1 is the number of 2 in the n! +1;
*/
int lowestonepos (int n)
{
int ret = 0; Number of decomposition 2 in statistical n!
while (n)
{
n >>= 1;
ret = n;
}
return ret+1;
}

"solution two of question 2"
N! Contains the number of decomposition 2 and equals n minus the number of 1 in the binary representation of N. We can also solve them by this rule.
The following is an example of this rule, assuming N = 11011, then n! contains decomposition 2 of the number of N/2 + N/4 + n/8 + n/16 + ...
Copy Code code as follows:

That is: 1101 + 110 + 11 + 1
= (1000 + 100 + 1)
+ (100 + 10)
+ (10 + 1)
+ 1
= (1000 + 100+ 10 + 1) + (100 + 10 + 1) + 1
= 1111 + 111 + 1
= (10000-1) + (1000-1) + (10-1) + (1-1)
= number of 1 in 11011-n binary representation

Summary
The binary number n of any length m can be expressed as N = b[1] + b[2] * 2 + b[3] * + ... + b[m] * 2 (m-1), where b [i] represents the number (1 or 0) on the first digit of the binary number. Therefore, if the lowest bit b[1] is 1, then the n is odd, and the other is even, dividing it by 2 is equal to the whole binary number to the low displacement one bit.
Related Topics
Given an integer n, determine whether it is a Fang of 2 (:n>0&& (n& (n-1)) ==0).
--------------------------------------------------------------------------------
[1] This rule invites the reader to prove itself (hint n/k, equal to 1, 2, 3, ..., the number of numbers in n that can be divisible by k).

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.