鸟语天空
利用斯特林公式对阶乘近似求值
post by:追风剑情 2016-12-7 13:55

斯特林公式

1111111.jpg

示例代码

using System;

namespace TTTest
{
    class Program
    {
        static void Main(string[] args)
        {
            double n = 10;
            Console.WriteLine(string.Format("精确求值: {0}的阶乘等于{1}", n, Factorial(n)));
            Console.WriteLine(string.Format("近似求值: {0}的阶乘等于{1}", n, AsyFactorial(n)));

            Console.Read();
        }

        private static double Factorial(double n)
        {
            double ret = n;
            for (double i = n - 1; i > 1; i--)
                ret *= i;
            return ret;
        }

        //利用斯特林公式对阶乘近似求值
        private static double AsyFactorial(double n)
        {
            double ret = Math.Sqrt(2 * Math.PI * n) * Math.Pow(n / Math.E, n);
            return ret;
        }
    }
}

 

运行测试

22222.png

 

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容