我试图用 C 表示以下数学表达式:
P(n) = (n!)(6^n)
当n
= 156 时,程序应该计算表达式的答案。我试图用 C 语言创建程序,但它无法产生答案。答案大约是 10^397。该程序使用 2 个对数恒等式。它还利用斯特林近似来计算大阶乘。
我怎样才能让它产生正确的答案,你对我如何改进代码有什么建议吗?(我对编程很陌生):
#include <math.h>
typedef unsigned int uint;
int main()
{
uint n=156; // Declare variables
double F,pi=3.14159265359,L,e=exp(1),P;
F = sqrt(2*pi*n) * pow((n/e),n); // Stirling's Approximation Formula
L = log(F) + n*log(6); // Transform P(n) using logarithms - log(xy) = log(x) + log(y) and log(y^n) = n*log(y)
P = pow(e,L); // Transform the resultant logarithm back to a normal numbers
}
谢谢!:)