' ****************************************************** ' * ' * Name: prg-calc-sums.vbs ' * ' * Design Phase: ' * Author: John Miner ' * Date: 06/09/2011 ' * Purpose: Calculate summations from n to m ' * using different techniques. ' * ' ****************************************************** ' ' Algorithm: ' ' ' Declare all variables ' Option Explicit ' ' Calculate sums by tail recursion ' Public Function SumsByTail(n, m, v) ' Display message wscript.echo "SumsByTail " wscript.echo " n: " & cstr(n) wscript.echo " m: " & cstr(m) wscript.echo " v: " & cstr(v) wscript.echo " " ' reached lower number if (m < n) then SumsByTail = v ' self call & calc v else SumsByTail = SumsByTail(n, m - 1, m + v) end if End Function ' ' Calculate sums by regular recursion ' Public Function SumsByRegular(n, m) ' Display message wscript.echo "SumsByRegular " wscript.echo " n: " & cstr(n) wscript.echo " m: " & cstr(m) wscript.echo " " ' reached lower number if (n > m) then SumsByRegular = 0 ' self call & calc at end of function else ' Save result to temp Dim tmp tmp = 0 tmp = n + SumsByRegular(n + 1, m) ' Display the result wscript.echo "ret call (" & cstr(n) & ", " & cstr(m) & ") = " & cstr(tmp) wscript.echo " " ' Return the result SumsByRegular = tmp end if End Function ' ' Calculate sums by iteration ' Public Function SumsByLoop(n, m) ' Display message wscript.echo "SumsByIteration " wscript.echo " n: " & cstr(n) wscript.echo " m: " & cstr(m) wscript.echo " " ' Default value SumsByLoop = 0 ' Calculate sum while (n <= m) SumsByLoop = SumsByLoop + n n = n + 1 ' Display the loop counter wscript.echo " loop: " & cstr(n) wscript.echo " val: " & SumsByLoop wscript.echo " " wend End Function ' ' Main Program defined ' Public Sub Main(min, max) ' Exit if any errors On Error Goto 0 ' Declare variables Dim aryTime(6) Dim intResult ' Solve by tail recursion aryTime(1) = Timer intResult = SumsByTail(min, max, 0) aryTime(2) = Timer aryTime(0) = aryTime(2) - aryTime(1) ' Show results wscript.echo "sum " & cstr(min) & " to " & cstr(max) & " = " & intResult wscript.echo "elapsed time = " & cstr(aryTime(0)) wscript.echo " " wscript.echo " " ' Solve by regular recursion aryTime(3) = Timer intResult = SumsByRegular(min, max) aryTime(4) = Timer aryTime(0) = aryTime(4) - aryTime(3) ' Show results wscript.echo "sum " & cstr(min) & " to " & cstr(max) & " = " & intResult wscript.echo "elapsed time = " & cstr(aryTime(0)) wscript.echo " " wscript.echo " " ' Solve by iteration aryTime(5) = Timer intResult = SumsByLoop(min, max) aryTime(6) = Timer aryTime(0) = aryTime(6) - aryTime(5) ' Show results wscript.echo "sum " & cstr(min) & " to " & cstr(max) & " = " & intResult wscript.echo "elapsed time = " & cstr(aryTime(0)) wscript.echo " " wscript.echo " " End Sub ' ~ ' Main program call ' ~ ' Check the error object On Error Resume Next ' The main program Call Main(5, 1500) ' ~ ' Error handling ' ~ If Err.Number <> 0 Then wscript.echo "The following execution error occurred (" & cstr(Err.Number) & "-" & Err.Description & ")" wscript.quit End If