Two questions about factorial

Source: Internet
Author: User

Two questions about factorial

This article describes two issues related to factorial operations in two. Remember that you cannot calculate the result of factorial because it overflows. Also do not convert to string to do, because it is more troublesome. In general, we can use mathematical methods to convert results to n-related, not n! of the results.


1. Count n! There are several zeros at the end


Given an n, the end of the computed n! has several zeros. This problem if you put n! It must be unrealistic to calculate it. Like this at the end of the calculation has a few zeros, is to let you count n! The result is 10 how many times, isn't it ... Similar let's you calculate n! The result of the binary form is finally a few zeros, and even let you calculate n! is 2 times as many as these problems are the same.

Well, since we know to know n! is 10 how many times, because 10 is not prime, so we can not directly to the 1~n all to judge again, in the end how many number is a multiple of 10. This is because if there is 5 in a number, a number has a multiple of 2, then they can not be removed by 10, and they multiply on it ~ ~ ~ ~ ~ ~ ~ ~ ~ So later to note, this situation, must be 10 (or other number) converted to prime.

Well, the program is below, and some of the attention is still more important. Be sure to note that "some numbers not only contribute 1, because it can be 5 times apart, so have this sentence."

Why does it only calculate 5 instead of judging if it is a multiple of 2? This is because the number of natural numbers in a multiple of 2 in the natural number is much larger than the number of natural numbers in multiples of 5. Since 5 and 2 appear simultaneously, it is certainly the lesser of the two. The number of multiples of 2 is 10, and the number of multiples of 5 is two, so the requirement to meet the multiples of 10 must be 5.


#include <iostream>using namespace std; int count0 (int N) {int num = 0; for (int i = 1; I <= N; i++)  //Because it is factorial, so to traverse 1~n{int j = i; while (j% 5 = = 0)  //Can be 5 Except the number is counted {num++; J/= 5;   Some numbers not only contribute 1, because it can be 5 times apart, so there is this sentence}}return num; }int Main () {cout<<count0 (+) <<endl; return 0;}


If you want to calculate the result of the binary form has a few zeros, you can directly use the above program, because 2 is prime.


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


The requirement of the topic is to determine n! The binary representation of the lowest bit 1 position, such as 3! =6=0110, then the lowest bit binary is the second in the low position.

When the law of the problem is not visible, we can first make a few numbers to see:

1 = 0001: in 1th place;

2 = 0010: in 2nd place;

4 = 0100: in 3rd place;

8 = 1000: in 4th place.

......

Initial explanation, the number of n/2+1 is the answer, if not divisible by 2, then is 1.


int lowestone (int N) {int num = 0; while (n) {n >>= 1; num + = n;} return num; }



Two questions about factorial

Related Article

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.