Stirling Formula and its application

Source: Internet
Author: User
Tags acos

The Stirling formula is a mathematical formula used to obtain an approximate value of n factorial. Generally speaking, when N is very large, n factorial calculation is very large, so the Stirling formula is very useful, and even when N is very small, the value of the Stirling formula is very accurate.

The formula is:

The programming of the Stirling formula: N. =SQRT (2*pi*n) * (n/e) ^n; (Pi=3.1415926=acos ( -1.0), e=2.718)

Conversion of the Stirling formula: LgN. = (LG (2*PI) +LGN)/2+n* (Lgn-lge);//+1 is the length.

Always ask for the decimal length to change LG to LOG10.

Template code:

#include <bits/stdc++.h>
using namespace std;
const int PI = ACOs ( -1.0);
int main ()
{
    int n;
    while (Cin>>n)
    {
        double ans;
        Ans= (0.5*log (2*pi*n) +n* (log (n)-1))/log (a);
        cout<< (Long Long) ans+1<<endl;
    }
}


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.