' ****************************************************** ' * ' * Name: calc-primes-v1.vbs ' * ' * Design Phase: ' * Author: John Miner ' * Date: 05/31/2011 ' * Purpose: Calculate primes numbers from 1 to n. ' * ' ****************************************************** ' ' Algorithm: ' ' 1 - Read In Program Arguement (N) ' 2 - Write Arguement N To Log File ' 3 - Write 2 & 3 as first two primes to result file ' 4 - Set found prime count = 2 ' 5 - For M = 4 to N ' 6 - For O = 2 to SQRT(M) ' 7 - If M modulus O = 0, then non prime ' 8 - Otherwise, write prime to result file ' 9 - Increment found prime counter ' 10 - Write Number Of Primes Found To Log File ' ' Declare all variables ' Option Explicit ' ' Load the class libraries ' ExecuteGlobal CreateObject("Scripting.FileSystemObject").OpenTextFile(CurrentPath() & "mod-app-logging.vbs").ReadAll ExecuteGlobal CreateObject("Scripting.FileSystemObject").OpenTextFile(CurrentPath() & "mod-write-text-file.vbs").ReadAll ' ' Get The Current Program Path ' Function CurrentPath() CurrentPath = Replace(WScript.ScriptFullName, WScript.ScriptName,"") End Function ' ' Trial Division ' Public Sub TrialDivision(intMaxProspect) ' Exit Sub When Error Occurs On Error Goto 0 ' Start time Dim objStart objStart = Now() ' Display message wscript.echo "Starting program" wscript.echo objStart wscript.echo " " ' ~ ' Step 2 ' ~ ' Create the logging object Dim objLogs Set objLogs = New clsAppLogging ' Set file / app name objLogs.FileName = "prg-calc-primes-v1.log" objLogs.AppName = "CalcPrimes" ' Write to just app log objLogs.Message = "Max Integer = " & cstr(intMaxProspect) objLogs.Events = Event_Information objLogs.Write ' ~ ' Step 3 ' ~ ' Write out first two primes Dim strFile strFile = "c:\vbs-depot\primes\primes.csv" ' Create the object Dim objTxtFile Set objTxtFile = New clsWriteTxtFile ' Open the file objTxtFile.OpenDataFile strFile, ForWriting ' Line variable & string delimiter Dim strLine Dim strDel strDel = "," ' Header line strLine = "" strLine = strLine & "Line No" & strDel strLine = strLine & "Prime No" objTxtFile.PushData strLine ' First prime number strLine = "" strLine = strLine & "1" & strDel strLine = strLine & "2" objTxtFile.PushData strLine ' Second prime number strLine = "" strLine = strLine & "2" & strDel strLine = strLine & "3" objTxtFile.PushData strLine ' ~ ' Step 4 - 9 ' ~ ' Declare routine variables Dim intProspect Dim intDivisor Dim intFlag Dim intFound intFound = 2 ' Find primes between 5 and N For intProspect = 4 to intMaxProspect ' Prime until otherwise intFlag = True ' Perform trial division For intDivisor = 2 to SQR(intProspect) If (intProspect MOD intDivisor) = 0 Then intFlag = False Exit For End If Next ' Found a prime If intFlag = True Then ' Increment counter intFound = intFound + 1 ' X prime number strLine = "" strLine = strLine & cstr(intFound) & strDel strLine = strLine & cstr(intProspect) objTxtFile.PushData strLine End If Next ' Close the data file objTxtFile.CloseDataFile ' ~ ' Step 10 ' ~ ' Write to just app log objLogs.Message = "Primes Found = " & cstr(intFound) objLogs.Events = Event_Information objLogs.Write ' End time Dim objEnd objEnd = Now() ' Time in seconds Dim intElapsed intElapsed = datediff("s", objStart, objEnd) ' Write to just app log objLogs.Message = "Elapsed Time (s) = " & cstr(intElapsed) objLogs.Events = Event_Information objLogs.Write ' Display message wscript.echo "Ending program" wscript.echo objEnd wscript.echo " " ' Display message wscript.echo "Elapsed Time (s) = " & cstr(intElapsed) wscript.echo " " End Sub ' ' Main Program ' ' Check the error object On Error Resume Next ' ~ ' Step 1 ' ~ If (wscript.arguments.count = 0) Then wscript.echo "Usage: " wscript.echo " prg-calc-primes-v1 " wscript.echo " " wscript.quit End If ' ~ ' Step 2 - 10 ' ~ TrialDivision Wscript.Arguments(0) ' ~ ' Error handling ' ~ If Err.Number <> 0 Then wscript.echo "The following execution error occurred (" & cstr(Err.Number) & "-" & Err.Description & ")" Err.Clear wscript.quit End If