Calculates the number of digits of the factorial of a large number n (decimal) reproduced

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:

1234567891011121314151617181920212223242526272829303132 //输入一个数字n,请你计算该数的阶乘的十进制数的位数宽度//比如:3!=6, 则宽度为1//样例数据://n=3  输出1//n=32000  输出130271//n=1000000  输出5565709#include <string>#include <iostream>#include <iomanip>#include <stdio.h>#include <cmath>usingnamespacestd;intmain(){    longintn;    longinti;    doublesum;    while(scanf("%ld", &n)!=EOF)    {        sum=0.0;        for(i=2; i<=n; i++)        {            sum+=log10(i);        }        printf("%ld\n", (int)sum+1 );    }    return0;}

Category: Number theory, mind + logic + thinking

Calculates the number of digits of the factorial of a large number n (decimal) reproduced

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.