Two factorial Problems

Source: Internet
Author: User

Two factorial Problems

Two questions about factorial

This article introduces two problems related to the factorial operation. Remember, the factorial result cannot be calculated because it will overflow. Do not convert it to a string because it is troublesome. In general, we can use a mathematical method to convert the result to N, instead of N! .


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


Given an N, calculate N! There are several zeros at the end. If this problem occurs! It is unrealistic to calculate it. There are several zeros at the end of the calculation, that is, let you calculate N! The result is 10 times, isn't it... Similarly, let you calculate N! Even if you want to calculate N! These problems are the same as the number of times of 2.

Okay, now that we know that we want to know N! The number of times of 10. Because 10 is not a prime number, we cannot directly set ~ N is judged once, and the number of digits is a multiple of 10. This is because if there is 5 in a number and a number has a multiple of 2, each of them cannot be divided by 10, and they can be multiplied ~ Right ~~ In this case, you must convert 10 (or another number) to a prime number.

Well, the program is below, some of which are important. Note: "Some numbers contribute more than one, because they can be divided by five times, so this sentence is required ".

In this example, why do we only calculate 5 instead of determining if it is a multiple of 2? This is because the number of natural numbers in the natural number is much greater than the number of natural numbers in the number of multiples of 5. Since both 5 and 2 are expected to appear at the same time, it must be a relatively small Algorithm for both. The number of multiples of ratio 2 has 10, and the number of multiples of 5 has 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 a factorial, traverse 1 ~ N {int j = I; while (j % 5 = 0) // The number of values that can be divided by 5 is calculated as {num ++; j/= 5; // some numbers contribute not only one, because it can be divided by five times, so this sentence must be} return num;} int main () {cout <count0 (100) <endl; return 0 ;}


If there are several zeros in the binary form of the calculation result, you can directly use the above program, because 2 is a prime number.


2. N! In binary format.


The requirement of the question is to confirm N! In binary format, it indicates the position of the second digit 1, for example, 3! = 6 = 0110, then the binary value of the decimal bit is the second digit in the lower position.

When the rule of the problem cannot be seen, we can first list several numbers:

1 = 0001: at 1st bits;

2 = 0010: at 2nd bits;

4 = 0100: at 3rd bits;

8 = 1000: 4th bits.

......

It is preliminarily stated that the number of N/2 + 1 is the answer. If it cannot be divisible by 2, it is 1.


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



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.