Featured C/C ++ interview questions

Source: Internet
Author: User

How many factorial digits are there in 1000? How many consecutive zeros are there?
There is a factorial Formula
For example, 1000 factorial digits:
Log10 (1) + log10 (2) + · + long10 (1000) is rounded up and then added to 1
Or
Log (N !) = LnN! /Ln10
= (NlnN-N)/ln10
= 2565.7

There is a way to get the number of digits of the factorial result, but to calculate N LOG10, the time is not good, so give up this method:
Set the number of digits to x,
Column equation 10 ^ x = N !, Www.2cto.com
Then x = log10 (N !)
Then x = log10 (1) + log10 (2) + log10 (3) +... + log10 (N ),
The final x is rounded up.

//////////////////////////////////////// //////////////////////////////////////

Each time a 2 or 5 appears, there will be a 0 at the end.
The total number of 2 and 5 in 1000 is enough, and because 5 is less than 2, you only need to check the number of approx. 5 In the 1000 factorial. Similarly, only the number with 0 or 5 at the end is 5, so there are only 200 numbers in total, including 5. However, among them, there are 1000/25 = 40 numbers, including 2, 125/625 = 8 numbers, including 3,/= 1 numbers, including 4 5, so there are 200 + 40 + 8 + 1 = 249 5 in total, so there are 249 consecutive 0 in the result.

//////////////////////////////////////// /////////////////////////////////


By fengzhishang2019

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.