The---of reading notes in programming Beauty "don't be intimidated by factorial."

Source: Internet
Author: User
Tags integer division

Question one: Given an integer n, how many 0 are there at the end of the factorial of n? For example n = ten, n! = 362800,n! There are two 0 at the end.

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

Solution one of the problem one:

The simplest way is to put n! Figure out, you can know how many 0 at the end.

Solution two of problem one:

We think so, at the end of the 0 can be obtained from where, a multiple of 10, such as 10,20,100 .... Such, can contribute 0, there is 2 * 5 This can also get a 0, but we can find that 10 is actually from 5,

10 = 2 * 5,

20 = 4 * 5,

100 = 20 * 5

So, we can conclude, how many 0, just see how many of the n! factorization contains 5,. At this point, the successful transformation of the original problem into a more simple problem.

Using this principle, we propose two solutions to calculate the number of factor 5 in factorization:

1, the first method is very simple, is directly split 5 out of

int tot = 0; Number of factor 5

for (int i = 1;i <= n;i++)

{

int temp = i;

while (temp% 5 ==0)

{

tot++;

Temp/= 5;

}

}

2, the second method of tot = [N/5] + [n/5^2] + [n/5^3]+ ... See how much n divided by 5 is actually the number of factors that can be divisible by 5 in n!, and then each factor can contribute a 5, such as 26! , [26/5] = 5, there are 5,10,15,20,25 five can be divisible by 5. Then the factor divisible by 5^2 can contribute two 5, such as 25, so the number of factors to be divided by 5^2, and so on.

int tot = 0;

int c = 5;

while (c <= N)

{

Tot + = (tot/n);

C *= 5;

}

Question two:

We know that an integer division divided by 2 can be replaced by the right 1 bits, when a number can be divisible by 2, the lowest bit of the binary is 0, when the lowest bit is 1 is odd, not divisible by 2, so, we can draw a number can be divided by 2 how many times, it shows the number of binary lowest bit how many 0. In other words, this is the number of the 2 factor. And then there are:

The number of the number N factorial n! of the binary lowest bit 0 = [N/2] + [n/2^2] + [n/2 ^ 3]+ ....

The---of reading notes in programming Beauty "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.