N! Number of digits, N! Number of digits

Source: Internet
Author: User

N! Number of digits, N! Number of digits
1) Description:

N! (The factorial of N) is a very large number. The formula is: N! = N * (N-1) * (N-2) *... * 2*1 ). Now you need to know N! The number of digits in decimal format.

Input: Enter 1 positive integer N in each row. 0 <N <1000000

Output: For each N, output N! In decimal format.

Input: 320001000000

Output: 130271 5565709

2) algorithm analysis:

For any given positive integer a, assuming 10 ^ (x-1) <= a <10 ^ x, then it is clear that the number of digits of a is x, because log10 (10 ^ (x-1) <= log10 (a) <(log10 (10 ^ x) is the X-1 <= log10 (a) <x then (int) log10 (a) = X-1, that is, (int) log10 (a) + 1 = x that is, the number of digits of a is (int) log10 (a) + 1, we know that the number of digits of a positive integer a is equal to (int) log10 (A) + 1. Now we want to calculate the number of digits of the factorial of n: Suppose a = n! = 1*2*3 *...... * n, so what we need is (int) log10 (A) + 1, while log10 (A) = log10 (1*2*3 *...... n) (according to log10 (a * B) = log10 (a) + log10 (B) = log10 (1) + log10 (2) + log10 (3) + ...... + log10 (n) so the number of digits of the n factorial is equal to (int) (log10 (1) + log10 (2) + log10 (3) + ...... + log10 (n) + 13) source code: # include <stdio. h> # include <math. h> Int main () {

Int n, I;
Double;
While (scanf ("% d", & n )! = EOF)
{
A = 0;
For (I = 1; I <= n; I ++)
{
A = a + (log10 (I ));
}
Printf ("% d \ n", (int) a + 1 );
}
}

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.