日期:2014-05-19  浏览次数:21903 次

菜鸟学编程:C#如何实现e的x次方的计算?附上公式
e的x次方=1+x/1!+x的2次方/2!+x的3次方/3!+....+x的n次方/n!+....
其中:n!=1*2*3*...*n

怎么使用C#实现?

谢谢先!

------解决方案--------------------
好像系统有这个函数
System.Math.Exp
------解决方案--------------------
private double expower()
{
int n = 10;
double E = Math.E;
double x = 2;
double p1 = Math.Pow(E, x);
double p2 = 1;
for (int i = 1; i <= n; i++)
{
p2 += x / factorial(i);
}
return p2;
}

private long factorial(int n)
{
long n_factorial = 1;
for (int i = 1; i <= n; i++)
{
n_factorial = n_factorial * i;
}
return n_factorial;
}

按你的算法算的,但结果不对,你再看看吧
------解决方案--------------------
自己写算法可以这样,用两个简单的递归
int exp_e(int n)
{//求e的x次方
if(n=0)
{
return 1;
}
else
{
return ((x_n(x,n)/n_jie(n)) + exp_e(n-1));
}
}
int x_n(int x,int n)
{//x的n次方
int i,y=x;
for(i=1;i <=n;i++)
{
y *= x
}
return y;
}
int n_jie(int n)
{//求阶乘
if(n=1)
{
return 1;
}
else
{
return n*n_jie(n-1)
}
}
------解决方案--------------------
private double expower()
{
int n = 10;
double E = Math.E;
double x = 2;
double p1 = Math.Pow(E, x);
double p2 = 1;
for (int i = 1; i <= n; i++)
{
p2 += Math.Pow(x, i) / factorial(i);
}
return p2;
}

private long factorial(int n)
{
long n_factorial = 1;
for (int i = 1; i <= n; i++)
{
n_factorial = n_factorial * i;
}
return n_factorial;
}

少了个x的i次方,不好意思

------解决方案--------------------
double x = 10;
double n = 10;
double result = 0;

for (int i=1;i < n;i++)
{
//x的次方
double xSum = x;
for (int j=1;j < i;j++)
{
xSum = xSum*x;
}

//n!
double nSum = 1;
for (int j=1;j < i;j++)
{
nSum = nSum*j;
}

result += xSum / nSum;
}

this.TextBox1.Text = Convert.ToString(result + 1);

---------------------------------------------
EMail:bdbox@163.com 请给我一个与您交流的机会!