The number of factorial end 0 of N and the position of the last bit 1 in the binary representation

Source: Internet
Author: User



Problem One solution:
We know the number of factorial results at the end of the n is 0, which means that we do the multiplication of n from 1 when the number of 10
we can decompose this, that is, the decomposition of the Genesis from 0 to N, and then multiply these by the number of 10? Actually, we just have to figure out how many of them are 5? because in these decomposed factors, can produce 10 can only be multiplied by 5 and 2, because the number of 2 is greater than the number of 5, so
we just have to count the number 5. So the problem is that the numbers from 1 to n are decomposed into factors, and these factorsthe number of 5.
Python code
  
 
  1. def factorialnumberofzero(v_value):
  2. countoffive = 0
  3. for num in range(1, v_value + 1):
  4. value = num
  5. while ((value % 2) == 0) and value:
  6. value /= 2
  7. countoffive += 1
  8. print countoffive
  9. return countoffive




solution to problem two:So how do we think about the problem?
In fact, it is very simple, we just calculate its result is 2 of how many power multiples of the line, if the result is 8, then 8 is 2 3 power, that is 8 of the last 1 in the bottom 4th.
So how to count n!    How many powers is 2? Of course, we only have to count when the multiplication from a few 2 multiplied on it, or the first problem as the decomposition factor multiplied ah, see the total number of factors in 2. But is there an easy way?
Of course
Let's look at the 10! , then 10! How many 2 are there?
Oh, first we will 10/2=5 that is from 10 to 1 has 5 even
10, 8, 6, 4, 2
How many even numbers must be 2, and then we divide them by 2.5, 4, 3, 2, 1
These 5 numbers also have 5/2=2 an even number. 4, 2
we divide 4 and 2 by 2 to get
2, 1
There is a 2/1=1 an even number.
2
2/2=1 would have been gone.

Look at the algorithm:
  
 
  1. def factorialpositionoflastone(v_value):
  2. positioncount = 0
  3. while v_value:
  4. v_value >>=1
  5. positioncount += v_value
  6. print positioncount
  7. return positioncount








From for notes (Wiz)

The number of factorial end 0 of N and the position of the last bit 1 in the binary representation

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.