Question

I have the code snippet below to tell if a number is prime or not in my vb project.

Public Sub CheckButton_Click(sender As Object, e As EventArgs) Handles CheckButton.Click

        Dim myNumber As Integer, myDivider As Integer
        myNumber = Integer.Parse(PrimeTextBox.Text)

        Select Case myNumber
            Case Is <= 0
                MessageBox.Show("I'm only accepting values above 0. :p")
                Exit Sub
        End Select

        For myDivider = 2 To (myNumber - 1)
            If myNumber Mod myDivider = 0 Then
                MessageBox.Show(" " & myNumber & " is not a prime Number")
                Exit For
            End If
        Next

        If (myDivider > (myNumber / myDivider)) Then
            MessageBox.Show(" " & myNumber & " is a prime number")
        End If
End Sub

I understand all that's going on up until the lines:

If (myDivider > (myNumber / myDivider)) Then

        MessageBox.Show(" " & myNumber & " is a prime number")

    End If

I just can't understand what the lines above do, all I know is when I remove these lines from my code, numbers like 9, 21, 99 ... are all detected as prime numbers, which is incorrect.

I understand that the first For loop checks if the number typed in by the user is evenly divisible by any number from 2 to the number - 1 cause obviously, a prime number can be divided by only itself and 1.

I'd like someone to explain to me clearly what those lines do exactly in my code, for now I only know it helps to check if numbers like 9, 99... are prime or not. Thanks.

Was it helpful?

Solution

We can rewrite the math in that condition to get a different view of what it does:

If (myDivider > (myNumber / myDivider)) Then

is the same as:

If (myDivider * myDivider > myNumber) Then

which is the same as:

If (myDivider > Math.Sqrt(myNumber)) Then

So, the condition checks if the divider is larger than the square root of the number.

The loop before that code doesn't use that fact, but you only need to check for an even divider up to the square root of the number. If the divider is larger than that, it means that the loop completed without finding an even divider, and the number is a prime.

A condition that would make more sense looking at the rest of the code would be to check for the value that the variable has after the loop:

If (myDivider = myNumber) Then

Another alternative would be to use End Sub instead of End For in the loop, then the condition after the loop is not needed at all.

OTHER TIPS

All right.. that second loop seems to be a misplaced optimization to the prime number checking algorithm. You could probably just write:

Public Sub CheckButton_Click(sender As Object, e As EventArgs) Handles CheckButton.Click

    Dim myNumber As Integer, myDivider As Integer
    myNumber = Integer.Parse(PrimeTextBox.Text)

    Select Case myNumber
        Case Is <= 0
            MessageBox.Show("I'm only accepting values above 0. :p")
            Exit Sub
    End Select

    Dim myDividerLimit As Integer
    myDividerLimit = CInt(Math.Floor(Math.Sqrt(myNumber)))

    For myDivider = 2 To myDividerLimit
        If myNumber Mod myDivider = 0 Then
            MessageBox.Show(" " & myNumber & " is not a prime Number")
            Exit Sub
        End If
    Next

    MessageBox.Show(" " & myNumber & " is a prime number")
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top