回 帖 发 新 帖 刷新版面

主题:求助高手

求e=1+1/2!+1/3!+1/4!+…+1/n!
注:N<=50

回复列表 (共5个回复)

沙发

你用的是什么语言?

板凳

I have one similar in VBA code in my hand. I just copy it here. You may refer to it and change it to Pascal.... I forgot the syntax in Pascal.

e^x = 1 + x^1/1! + x^2/2! + x^3/3! + ?+ x^n/n!

I may write your problem in Pascal later if I have time.

VBA:

Option Explicit

Function Factorial(n)
Dim Fact As Single, power As Single, i As Single
i = 1
power = 1
Do
Fact = power * i
i = i + 1
power = Fact
If i >= n + 1 Then Exit Do
Loop
Factorial = Fact
End Function

Function Maclaurin(x, n)
Dim sum As Single, i As Integer
sum = 1
For i = 1 To n
sum = sum + x ^ i / Factorial(i)
Next
Maclaurin = sum
End Function

3 楼

我用的是TP啊

4 楼

写了两个functions,好久没有写pascal了,可能有语法错误。。。手头上没有软件调试,你试试看看行不行吧。
你求e, 就调用函数  maclaurin(1,n), n<50。当然maclaurin函数里面要调用fact,所以两个都放到程序里面。

function fact(n:integer);
  var i,product:integer;
  begin
    product:=1;
    for i:=1 to n do product:=product*i;
    fact:=product;
  end;

Function Maclaurin(x,n:integer);
var sum:real;i:integer;
begin
  sum := 1;
  For i := 1 to n do sum := sum + x ^ i / Fact(i);
  Maclaurin := sum;
End;

惨了,好像pascal没有这个"^"玩意的吧?你想想怎么把它修正就是了。。

5 楼

好象只有指针有‘^’这个符号

我来回复

您尚未登录,请登录后再回复。点此登录或注册