Understanding Computer Programming

Osher Lifelong Learning Institute
University of Illinois, Urbana-Champaign

Scott Badman, Instructor


Topic: Prime algorithm implemented in Object Oriented Visual Basic

February 25, 2016


In addition to the code below, Visual Basic generates a lot of code depending on the drag-and-drop design of the Window and its components.


Public Class PrimeWindow

     Dim number As Integer
     Dim answer As String
     Dim tester As PrimeTester
     Dim result As PrimeTester.PrimeResult

     Private Sub NumberInput_KeyUp(ByVal sender As System.Object,_
       ByVal e As System.Windows.Forms.KeyEventArgs) Handles NumberInput.KeyUp

         AnswerLabel.Text = ""

         If e.KeyCode = Keys.Enter Then

             If Int32.TryParse(NumberInput.Text, number) = 0 Then

                 AnswerLabel.Text = "Invalid input"

             Else

                 tester = New PrimeTester()
                 result = tester.Test(number)

                 AnswerLabel.Text = result.ToString()

                 If result = PrimeTester.PrimeResult.NOTINRANGE Then
                     AnswerLabel.Text = "Number out of range"
                 ElseIf result = PrimeTester.PrimeResult.ISPRIME Then
                     AnswerLabel.Text = number.ToString + " is a prime."
                 ElseIf result = PrimeTester.PrimeResult.NOTPRIME Then
                     AnswerLabel.Text = number.ToString + " is not a prime."
                 End If

             End If

         End If

     End Sub

     Private Sub PrimeWindow_Load(ByVal sender As System.Object,_
       ByVal e As System.EventArgs) Handles MyBase.Load

     End Sub

End Class


Public Class PrimeTester
     Private prime As Boolean
     Private inRange As Boolean

     Private Const LIMIT As Integer = 2000000000

     Public Enum PrimeResult As Integer
         ISPRIME = 1
         NOTPRIME = 0
         NOTINRANGE = -1
     End Enum

     Public Sub New()
         prime = False
         inRange = False
     End Sub

     Public Function Test(ByVal number As Integer) As PrimeResult

         If number < 0 Or number > LIMIT Then
             Return PrimeResult.NOTINRANGE
         End If

         DetermineIfPrime(number)

         If prime = True Then
             Return PrimeResult.ISPRIME
         Else
             Return PrimeResult.NOTPRIME
         End If

     End Function

     Private Sub DetermineIfPrime(ByVal number As Integer)
         Dim remainder As Integer
         Dim divisor As Integer

         Dim finder As New ModuloFinder

         If number = 0 Or number = 1 Then
             prime = False
         Else
             prime = True

             divisor = 2

             remainder = finder.Modulo(number, divisor)

             If remainder = 0 Then
                 prime = False
             End If

             divisor = 3

             While prime = True And divisor <= Math.Sqrt(number)

                 remainder = finder.Modulo(number, divisor)

                 If remainder = 0 Then
                     prime = False
                 End If

                 divisor = divisor + 1

             End While

         End If
     End Sub

End Class


Public Class ModuloFinder
     Public Const NOTINRANGE As Integer = -1

     Public Sub New()
     End Sub

     Public Function Modulo(ByVal number As Integer, ByVal divisor As Integer) As Integer

         Dim intermediate As Integer
         Dim remainder As Integer

         If number < 0 And divisor <= 0 Then

             remainder = NOTINRANGE

         Else

             intermediate = number

             While intermediate >= divisor
                     intermediate = intermediate - divisor
             End While

             remainder = intermediate

         End If

         Return remainder

     End Function

End Class


Understanding Computer Programming

Osher Lifelong Learning Institute
University of Illinois, Urbana-Champaign

Scott Badman, Instructor