Computes the number of digits of the factorial of a large number n (decimal) (log I accumulation method)

Source: Internet
Author: User

Input:

Enter 1 positive integers per line n, (0<n<1000 000)

Output:

For each n, the number of (decimal) digits of the output n!.

Analysis:

This problem uses brute force method. By definition, direct solution!

The so-called n! decimal digits, is log (n) +1, according to the mathematical formula is: n!=1*2*3*.....*n;

LG (n!) =LG (2) +......LG (n);

Code:

Enter a number n, and you calculate the number of decimal digits of the factorial of the digit width//For example: 3! =6, the width is 1//sample data://n=3  output 1//n=32000  output 130271//n=1000000  output 5565709#include <string> #include < iostream> #include <iomanip> #include <stdio.h> #include <cmath>using namespace Std;int main () {    long int n;    long int i;    Double sum;    while (scanf ("%ld", &n)!=eof)    {        sum=0.0;        for (i=2; i<=n; i++)        {            sum+=log10 (i);        }        printf ("%ld\n", (int) sum+1);    }    return 0;}

Computes the number of digits of the factorial of a large number n (decimal) (log I accumulation method)

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.