View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
William Benson[_2_] William Benson[_2_] is offline
external usenet poster
 
Posts: 230
Default Need code to sum a series

Or simpler yet:


Function CalcSeries(WhereToStop As Long) As Double
Dim i As Long
'1/(1*1) + 1/(1*2) + 1/(1*2*3)+1/(1*2*3*4) + ... + to the 150th term.
For i = 1 To WhereToStop
CalcSeries = CalcSeries + 1 / WorksheetFunction.Fact(i)
Next i
End Function


"William Benson" wrote in message
...
You can have a user defined function I suppose, store it in Personal.XLS
and call it with
=Personal.XLS!CalcSeries entered as the cell formula

Function CalcSeries(WhereToStop As Long)
Dim i As Long
Dim RunningSum As Double
'1/(1*1) + 1/(1*2) + 1/(1*2*3)+1/(1*2*3*4) + ... + to the nth

For i = 1 To WhereToStop
RunningSum = RunningSum + 1 / WorksheetFunction.Fact(i)
Next i
CalcSeries = RunningSum

End Function

"davidm" wrote in
message ...

I have been grappling with the algorithm to sum up the following series,
arguably a very awkward one.

1/(1*1) + 1/(1*2) + 1/(1*2*3)+1/(1*2*3*4) + ... + to the 150th term.

It would appear something between a recursive and For/Next construct
might be required but I can't quite get my head around it. Here was my
attempt (using a Function) which failed with Run time "out of stack
space" error.

Function SumSeries(n)
For i = n to 1 step -1
s = s + 1/(i*i-1*Sumseries(i))
Next
SumSeries = s
End Function

Sub test()
Msgbox Sumseries(150)
End Sub


--
davidm
------------------------------------------------------------------------
davidm's Profile:
http://www.excelforum.com/member.php...o&userid=20645
View this thread:
http://www.excelforum.com/showthread...hreadid=385467