2nd Chapter The charm of numbers-don't be intimidated by factorial

Source: Internet
Author: User

Don't be intimidated by factorial. Description

Factorial (factorial) is a very interesting function, but a lot of people are afraid of it, let's look at two factorial-related problems:

Question 1. Given an integer n, then the factorial n of n! How many 0 are there at the end? Example: n=10,n! =3 628 800,n! There are two 0 at the end.

Question 2. Ask N! The position of the lowest bit 1 in the binary representation.

Analysis and Solution

Some people encounter such a topic will think: is not to complete the calculation of n! The value? What if it overflows? In fact, the problem becomes simple if we consider the angle of "which number can be multiplied by 10".

Consider first, if n! = kx10m, and K cannot be divisible by 10, then n! There are M 0 at the end. Think again about n! Perform the mass factor decomposition, N! = (2x) x (3y) x (5z) ..., because ten = 2x5, so m is only associated with 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 the number that can be divisible by 2 is much higher than the number that can be divisible by 5, so the formula is simplified to M = Z.

According to the above analysis, just calculate the value of Z, you can get n! The number of the end 0.

Question 1 "solution one"

The code is as follows:

 PackageChapter2shuzizhimei.jiecheng;/*** Don't be intimidated by factorial * calculate factorial at the end of a few 0
* "Solution One" *@authorDell **/ Public classJieCheng1 {/*** Calculates the number of factorial end 0 *@paramN Calculation n! * @returnnumber of end 0*/ Public Static intCountintN) { intnum = 0;//records the number of 0 at the end, or 5 of the dimension (due to the number of >=5 of the 2 of the number of times in the order) for(inti=1;i<=n;i++){ intj =i; while(j%5==0) {num++; J/= 5; } } returnnum; } Public Static voidMain (string[] args) {intn = 10; SYSTEM.OUT.PRINTLN (n+ "! The number of the end 0 is: "+count (n)); }}

The results of the program run as follows:

10! The number of the end 0 is: 2
Question 1 "solution two"

The code is as follows:

1  PackageChapter2shuzizhimei.jiecheng;2 /**3 * Don't be intimidated by factorial4 * Calculate factorial at the end of a few 05 * "Solution two"6  * @authorDell7  *8  */9  Public classJieCheng2 {Ten     /** One * Calculates the number of factorial end 0 A      * @paramN Calculation n!  -      * @returnnumber of end 0 -      */ the      Public Static intCountintN) { -         intnum = 0;//records the number of 0 at the end, or 5 of the dimension (due to the number of >=5 of the 2 of the number of times in the order) -          while(n!=0){ -num + = N/5; +n = N/5; -         } +         returnnum; A     } at      Public Static voidMain (string[] args) { -         intn = 10; -SYSTEM.OUT.PRINTLN (n+ "! The number of the end 0 is: "+count (n)); -  -     } -  in}

The results of the program run as follows:

10! The number of the end 0 is: 2
Question 2: Ask N! The position analysis and solution of the lowest bit 1 in the binary representation:

At first glance, it seems that problem two has nothing to do with the problem. However, we think in a different way, the lowest bit 1 in the binary is definitely 0, then the position of the lowest bit 1, that is, to find the lowest bit 1 after the number of 0, and this, and the problem is the same, but one is a decimal table, one is a binary representation. Here, in all numbers less than N, a multiplier of 2 contributes a multiple of one 0,4 to another 0, and so on.

The key: Since the binary representation is actually a 2-based representation, each occurrence of a 2, the end will have a 0. So just find the number of factor 2 in n!.

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 1.

Because n! Contains the number of factorization 2, equal to N/2 + N/4 + n/8 + n/16 + ..., according to the above analysis, to obtain a specific algorithm, as follows:

1  PackageChapter2shuzizhimei.jiecheng;2 /**3 * Don't be intimidated by factorial4 * "Problem II" seeking n! The position of the lowest bit 1 in the binary representation5 * "Solution One"6  * @authorDELL7  *8  */9  Public classJieCheng1 {Ten     /** One * Calculation n! The position of the lowest bit 1 in the binary representation A      * @paramN -      * @returnposition (from rear forward number) -      */ the      Public Static intLocateintN) { -         intnum = 1;//Record n! Number of medium-quality factor 2 +1 -          while(n!=0){ -N >>= 1; +num + =N; -         } +         returnnum; A     } at      Public Static voidMain (string[] args) { -         intn = 3; -System.out.println ("n! The position of the lowest bit 1 in the binary representation is: "+locate (n)); -     } -  -}

The results of the program run as follows:

N! The position of the lowest bit 1 in the binary representation is: 2
"Solution two of question 2"

N! Contains the number of factorization 2, also equals n minus n in the binary representation of 1. We can also solve it by this law.

The following is an example of this rule, assuming that N = 11011 (binary representation), then n! contains factorization 2 of the number is N/2 + N/4 + n/8 + n/16 + ...

i.e.: 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)
= the number of 1 in the 11011-n binary representation

Then there are the following algorithms:

1  PackageChapter2shuzizhimei.jiecheng;2 /**3 * Don't be intimidated by factorial4 * "Problem II" seeking n! The position of the lowest bit 1 in the binary representation5 * "Solution two"6  * @authorDELL7  *8  */9  Public classJieCheng2 {Ten     /** One * Calculation n! The position of the lowest bit 1 in the binary representation A      * @paramN -      * @returnposition (from rear forward number) -      */ the      Public Static intLocateintN) { -         intm =N; -         intnum = 0;//record the number of binary representations of N in 1 -          while(m!=0){ +num + = m&0x01; -M >>= 1; +         } A          at         returnN-num+1; -     } -      Public Static voidMain (string[] args) { -         intn = 3; -System.out.println ("n! The position of the lowest bit 1 in the binary representation is: "+locate (n)); -     } in  -}

The results of the program run as follows:

N! The position of the lowest bit 1 in the binary representation is: 2
Summary

A binary number N of any length m can be represented as N = b[1] + b[2] * 2 + b[3] * + + ... + b[m] * 2 (m-1), where b [i] represents the number (1 or 0) of 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.

Related Topics

  Given the integer n, determine whether it is a operational of 2 (answer hint:n>0&& ((n& (n-1)) ==0).

The topic is actually to determine whether the binary representation of n is only a 1, the better algorithm is as follows:

1  PackageChapter2shuzizhimei.jiecheng;2 /**3 * Don't be intimidated by factorial4 * "Related issues" given an integer n, determine if it is 2 operational5  * @authorDELL6  *7  */8  Public classJieCheng3 {9     /**Ten * Determine if n is 2 operational One      * @paramN A      * @return -      */ -      Public Static BooleanIsThePower2 (intN) { the         if(n>0&& (n& (n-1)) ==0){ -             return true; -}Else{ -             return false; +         } -     } +      Public Static voidMain (string[] args) { A         intn = 5; at         if(IsThePower2 (n)) -System.out.println (n+ "is a power of 2! "); -         Else -System.out.println (n+ "is not a power of 2! "); -     } -  in}

The results of the program run as follows:

5 is not a power of 2!

2nd Chapter The charm of numbers-don't be intimidated by 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.