Factorial-related issues

Source: Internet
Author: User

Definition:A positive integer. factorialEnglish factorial) is the product of all positive integers less than and equal to the number, and has a 0 factorial of 1.    The factorial writing n! of the natural number N. namely N!=1x2x3x...xn. Factorial can also Recursive mode definition: 0!=1,n!= (n-1)!xn. question 1, given an integer n, then the factorial n of n! How many 0 are there at the end? For example: n = ten, n! = 3628800,n! There are two 0 at the end. parsing: Add complete to calculate n! Factorial, it is very likely that an overflow occurs. We can start from another point of view, " which numbers multiply to ten”。 N! = k * 10 m Time Square, K cannot be divisible by 10, then n! At the end there's M 0. to n! Perform the mass factor decomposition, N! X-=2 X-X3 of the Y-X5 of the Z-square X. ..., because of 10=2x5, so M is only related to X and z, each pair of 2 and 5 can be multiplied by one 10, so m = min (x,z). It is not difficult to see that X is greater than or equal to Z, because a book divisible by 2 is more frequent than the number divisible by 5, so it is formulated Jianwei m = z(The number of 5 that can be divided evenly); Method 1:The most straightforward method is to calculate the exponent of 5 in the factorization of I (1,2,3,......,n) and then sum it up.
/*** Calculates the number of digits at the end of the factorial result of N 0 *@paramN to find the number of factorial *@returnThe number of digits at the end of the factorial result of N 0*/  Public Static intGetlowzeronum (intN) {intRET = 0; intJ;  for(inti = 1; I <=n; i++) {J=i;  while(j% 5 = = 0) {ret++; J= J/5; }  }  returnret;}

Method 2: Formula method formula: Z = "N/5" + "N/5 squared" + "n/5 cubic" + ..., (Don't worry about it an infinite operation, because there is always a K, which makes 5 K-square > N, "n/5 k-th" = 0. In the formula, "N/5" means no more than N of the number of 5 in multiples of the contribution of a 5, "n/5 squared" means no more than N of the number, 5 of the ordinary multiples in the contribution of a 5 ... The code is as follows:
/**   * Calculates the number of digits at the end of the factorial result of n @param  n  for factorial @return  n factorial results at the end of the 0 digits   */publicstaticint getLowZeroNum2 (int  N) {   int ret = 0;    while (0! = N)  {   + =N/5   ; /= 5;  }   return ret;}


question 2, the position of the lowest bit 1 in the binary representation of the n!Divide a 2 binary number by 2 to determine if the last bits is 0, or 0 to move the binary right one bit, that is, the quotient value, and conversely, if 1, the binary number is odd and cannot be divisible by 2. So This problem is actually equivalent to seeking n! Contains the number of factorization 2. That answer equals n! Contains the number of factorization 2 plus 1Solution 1:N! Contains the number of factorization 2, equal to "N/2" + "N/4" + "N/8" + "n/16" + ..., the specific algorithm is as follows:
/*** n the factorial of the lowest bit 1 position *@paramN to find the number of factorial *@returnThe position of the least bit 1 of the factorial of n*/  Public Static intGettheposition (intN) {intRET = 0;  while(0! =N) {n= N >> 1;//move right one bit, equal to decimal divided by 2ret+ = N;//number of multiples of 2 less than the current n  }  returnret;}

Test:
 Public Static void Main (string[] args) {  // Test calculates the factorial result of n the number of digits at the end of the 0  int N = 5;   + "Number of digits at the end of the factorial result 0:" + Factorial.getlowzeronum (N  )); + "Number of digits at the end of the factorial result 0:" + factorial.getlowzeronum2 (N   )); // the position  of the least bit 1 of the factorial of the test n SYSTEM.OUT.PRINTLN (n + "n" of the factorial of the lowest bit 1 position: "+ factorial.gettheposition (n));}

Results:5 factorial results at the end of the 0 digits: 15 of the factorial result at the end of the 0 digits: the factorial of the 15N the lowest bit 1 position: 3 Summary:Any binary number n with a length of M can be represented as N = b[1] +b[2]*2 + b[3]*4 + ... + b[m]*2 m-1, where B[i] represents the number (1 or 0) on this binary number I bit. Therefore, if the lowest bit b[1] is 1, then n is an odd number, and the inverse is even, dividing it by 2, which is equal to the entire binary to the low displacement one bit.

Factorial-related issues

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.