Programmer interview 100 Question 8: do not be intimidated by the factorial (Binary indicates the position of the second digit 1)

Source: Internet
Author: User

Factorial is a very interesting function, but many people are afraid of it. Let's look at two problems related to Factorial:

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

2. Ask n! In binary format.

Some people may think about this question: is it necessary to calculate n completely! ? What if overflow occurs? As a matter of fact, if we consider "which numbers can be multiplied to get 10", the problem becomes simple.

First consider, if n! = K × 10 ^ m, and K cannot be divisible by 10, so n! There are M 0 at the end. Consider n again! Perform prime factor decomposition, n! = (2 ^ X) × (3 ^ y) × (5 ^ Z )..., Since 10 = 2 × 5, M is only related to X and Z. Each pair of 2 and 5 can be multiplied to get a 10, so m = min (x, z ). It is not hard to see that X is greater than or equal to Z, because the number of integers 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, as long as the value of Z is calculated, We can get n! The number of zeros at the end.

[Solution 1 of Question 1]

To calculate Z, the most direct method is to calculate I (I = 1, 2 ,..., N) in the formula decomposition of the 5 index, then sum:

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 2 of Question 1]

Formula: z = [N/5] + [N/5 ^ 2] + [N/5 ^ 3] +... (Don't worry that this will be an infinite operation, because there is always a K, making 5 ^ K> N, [N/5 ^ K] = 0 .)

In the formula, [N/5] indicates the contribution of a factor of 5 not greater than N, [N/5 ^ 2] indicates that the number not greater than the number of N is a multiple of 5 ^ 2 and then contributes 5 ,...... The Code is as follows:

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

Question 2 requires n! In binary format. Given an integer N, evaluate n! Which of the following is the first digit of binary representation? For example, given n = 3, n! = 6, so n! In binary format (1 010.

To get a better solution, you must first convert the question.

First, let's look at the calculation process and result of dividing the next binary number by 2.

Divide a binary number by 2. The actual process is as follows:

Determines whether the last binary is 0. If it is 0, shifts the binary number one to the right, that is, the business value (why). Otherwise, if it is 1, it indicates that the binary number is an odd number and cannot be divisible by 2 (this is why ).

Therefore, this problem is equivalent to finding n! Number + 1 containing prime factor 2. That is, the answer is equal to n! Add 1 to the number containing prime factor 2. Actually n! All are even, because there is a 2 in the prime factor, except 1, because the factorial of 1 is 1, an odd number, and the factorial of other numbers are even ..

[Solution 1 of Question 2]

Because n! Contains the number of prime factor 2, equal to n/2 + N/4 + N/8 + N/16 +... [1],

Based on the above analysis, the specific algorithm is obtained as follows:

/* Obtain n first! The number of 2 (because there is a 2, the number of digits is more than 1 0 ). Therefore, the seek position of 1 is n! 2 + 1; */INT lowestonepos (int n) {int ret = 0; // count n! Contains the number of prime factor 2 while (n) {n >=1; RET + = N;} return ret + 1 ;}

[Solution 2 of Question 2]

N! Contains the number of prime factor 2, which is equal to the number of 1 in the binary representation of N minus n. We can also solve this problem through this rule.

The following is an example of this rule. Suppose n = 11011, then n! The number of prime factor 2 is n/2 + N/4 + N/8 + N/16 +...

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

= (1-10000) + (1000-1) + (10-1) + (1-1)

= 11011-n binary indicates the number of 1

Summary
Any binary number of M can be expressed as n = B [1] + B [2] * 2 + B [3] * 22 +... + B [m] * 2 (S-1), where B [I] indicates the number (1 or 0) on the I-bit of the binary number ). Therefore, if the percentile B [1] is 1, it indicates that N is an odd number. If it is an even number, it is divided by 2, that is, the entire binary number is shifted to a low position.

Related Questions
Given the integer N, determine whether it is the power of 2 (answer prompt: n> 0 & (N & (n-1) = 0 )).

--------------------------------------------------------------------------------

[1] This rule should be proved by the reader (the prompt N/K is equal to 1, 2, 3 ,..., N ).

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.