N! (Factorial of N) Method for Solving Non-zero at the last position

Source: Internet
Author: User
Tags two factor

The problem is to ask about n! The last digit of is not 0, such as 3! = 6, the last non-0 digit is 6, 5! = 120, the last non-0 bit is 2. How can we quickly find the last non-0 bit?

The simplest idea is to find n first! And then obtain the last non-zero digit of the result. when N is relatively small, it can be tolerated, but when n reaches a certain scale, the time and space will not be too good. some tips are required here. since the last digit is not 0, we can first remove all numbers that have no effect on the result, such as multiples of 10. so first put n! The Factorization result is like 2 ^ A * 5 ^ B * C. at this time, we remove a factor B, A Factor 5, and a factor B, and the last non-zero position remains unchanged. (n! The factor of 2 must not be less than the factor of 5 ).

The result of our request is (2 ^ (a-B) * C) % 10. by (A * B) % 10 = (a % 10) * (B % 10) % 10 We Can Get (2 ^ (a-B) % 10) * (C % 10) % 10. Since C does not generate a null value, only the lowest digit of C is reserved. therefore, we can convert C to the least bit of the result obtained by multiplying 1, 3, 7, and 9 (because the multiplication of 1, 3, 7, and 9 does not produce the least non-0 bits, therefore, removing the high position does not affect the result. At the same time, 1 * n = n can remove the 1 factor ).

2, 3, 7, 9 factor rules are as follows:

2 ^ 1 = 2, 2 ^ 2 = 4, 2 ^ 3 = 8, 2 ^ 4 = 16-> (6), 2 ^ 5 = 32-> (2)

3 ^ 0 = 1, 3 ^ 1 = 3, 3 ^ 2 = 9, 3 ^ 3 = 27-> (7), 3 ^ 4 = 81-> (1)

7 ^ 0 = 1, 7 ^ 1 = 7, 7 ^ 2 = 49-> (9), 7 ^ 3 = 343-> (3 ), 7 ^ 4 = 2401-> (1)

9 ^ 0 = 1, 9 ^ 1 = 9, 9 ^ 2 = 81-> (1), 9 ^ 3 = 729-> (9 ), 9 ^ 4 = 6561-> (1)

They all take 4 as the cycle, so we only need to find the number of factors 2, 5, 3, 7, and 9.

First, we need to calculate 2, 5 at n! Number in. each of the two factors has at least one even number, and each number in the series/2, and the even number has a two factor. until n = 1 or n = 0 ends. the five-factor method is the same. the Code is as follows:

int getFactor_2_5(int n, int f){    int ret=0;    while(n>0){        ret+=n/f;        n/=f;    }    return ret;}
What are the numbers of factors 3, 7, and 9? For 1, 2, 3, 4... n-1, N, the number of numbers ending with 3, 7, and 9 is

N/10 + (n % 103f? 1-0), (F = 3, 7, 9 ).

At the same time, we can obtain a new sequence for the odd number series/5, which also has 3, 7, 9 factors. For the even number series/2, we can also obtain the new series with 3, 7, 9 factors, the total number of factors 3, 7, and 9 can be obtained by adding all the factors 3, 7, and 9. after obtaining the number of factors 3, 7, 9, we can convert all of them to the number of factors 3. because 9 = 3*3 (3 ^ 2), 7 = (3*3*3 (3 ^ 3) % 10, set F3, F7, F9 to 3, 7, number of 9 factors, all converted to the number of Factor 3 is F3 + 2 * F9 + 3 * f7.

So we can use recursion to calculate the number of factors 2, 3, 5, 7, and 9 at the same time. The Code is as follows:

void getFactor(int n){    if(n==0)        return;    for(int m=n; m>0; m/=5){        int t=m/10, r=m%10;        f3+=t+(r>=3);        f5+=t+(r>=5);        f7+=t+(r>=7);        f9+=t+(r>=9);    }    f2+=n/2;    getFactor(n/2);}
Two arrays are available to indicate loops:

  1. Int P2 [4] = {6, 2, 4, 8 };
  2. Int P3 [4] = {1, 3, 9, 7 };

Therefore, the result is (1). When the numbers of 2 and 5 factors are the same, they are only related to 3 factors, and the result is P3 [F3% 4] % 10;

(2) When factor 2 is greater than Factor 5, the result is also related to factor 3 and factor 2 (P2 [F2% 4] * P3 [F3% 4]). % 10.

Actually passed n! We can find the least non-zero position of the permutation and combination number NPM, C (n, m, the number of each factor minus the number of each factor below is the number of each factor in the result. note that the Factor 5 may be more than the factor 2. when the factor of 5 is more than the factor of 2, the unspecified value must be 5. the rest is the same as above.



N! (Factorial of N) Method for Solving Non-zero at the last position

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.