Visual Basic

Visual Basic is one of my favorite languages, despite the fact that it receives scathing remarks from some computer scientists. It's one of the most widely used development languages for database applications.

Technically speaking Visual Basic is a glue language or a scripting language that us used to combine components into an application. In most cases the significant programming for an application is done in C or C++, or some other compiled language, and the compiled units are combined using Visual Basic.

It is possible to use Visual Basic as a stand-alone command-line language, but I don't know anyone who uses it that way. This program was created and run under Visual Basic 6.0 (the last non-java-ized version of VB) and contains an interactive window. When the program runs you click the button to execute the algorithm and display the results in a text box.

The first line of this program, "Private Sub Command1_Click()" indicates that this code is the code behind a button named "Command1" and that it will be executed in response to a "Click" event.

Rather than supplying you with a simple text file, which would be of little value to you, the "actual code" for this version of SieveE is a zip file containing the entire Visual Basic project. Unzip these files and open the .vbp file in Visual Basic. (It should work in any version from 6.0 and up.)

Private Sub Command1_Click()


'/ Name: Peter M. Maurer

'/ Program: Sieve of Eratosthenes

'/ Due: Never

'/ Language: Visual Basic


'/ define the sieve data structure

Dim Candidates(1000) As Integer

Dim i As Integer

Text1.Text = ""

For i = 0 To 999

'/ everything is potentially prime until proven otherwise

Candidates(i) = 1

Next i

'/ Neither 1 nor 0 is prime, so flag them off

Candidates(0) = 0

Candidates(1) = 0

'/ start the sieve with the integer 0

i = 0

While i < 1000

'/ advance to the next un-crossed out number.

'/ this number must be a prime

While i < 1000 And Candidates(i) = 0

i = i + 1


'/ insure against running off the end of the data structure

If i < 1000 Then

'/ cross out all multiples of the prime, starting with 2*p.

Dim j As Integer

j = 2

While j * i < 1000

Candidates(j * i) = 0

j = j + 1


'/ advance to the next candidate

i = i + 1

End If


'/ all uncrossed-out numbers are prime (and only those numbers)

'/ print all primes

For i = 0 To 999

If Candidates(i) <> 0 Then

Dim TStr As String

TStr = i & " is prime" & Chr(13) & Chr(10)

Text1.Text = Text1.Text & TStr

End If

Next i

End Sub

Click Here for the actual code.