Im working on an app that takes two numbers(lower bound and upperbound)and determines all the prime numbers within the specific bounds, inclusive.

Public Class primeNumber

    Private Sub calculatePrimesButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculatePrimesButton.Click
        'declare variables
        Dim N As Long
        Dim LowerBound As Integer
        Dim UpperBound As Integer

        Dim i As Integer = 0
'user input
        LowerBound = Val(lowerboundTextBox1.Text)
        UpperBound = Val(upperboundTextBox2.Text)
 'display error message when user inputs an upperbound that is less than lower bound
        If UpperBound < LowerBound Then

            MessageBox.Show("Upper Bound cannot be less than Lower Bound", "Invalid Bounds", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

'declare an array that will store the prime numbers gotten





        For i = LowerBound To UpperBound

        If IsPrime(N) = True Then
        primeTextBox.Text = N

        End If


   Next




    End Sub

    Private Function IsPrime(ByVal number As Long) As Boolean

    If number = 2 Then
       Return True

    Else

        For i = 1 To number \ 2
            If number Mod i = 0 Then
                Return False
            End If
         Next
    End If

Return True
End Function
End Class

I know i need to get the statment that counts from 2 to the square root of the number, and use the mod op in the body of the for next loop. Ive done something similar to that but i think im missing the square root and someething else in my function, or is in my main code?

Recommended Answers

All 5 Replies

Line 48 in your function IsPrime was wrong For i = 1 To number \ 2 see this :

Private Function IsPrime(ByVal number As Long) As Boolean
        Dim i As Integer
        If number = 1 Then
            Return False
        ElseIf number = 2 Then
            Return True
        ElseIf number > 2 Then
            For i = 2 To number - 2
                If number Mod i = 0 Then
                    Return False
                End If
            Next i
        End If
        Return True
    End Function

When you cheking prime number, line 28 & 29 is wrong.

For i = LowerBound To UpperBound
    If IsPrime(i) = True Then
    primeTextBox.Text = primeTextBox.Text & " " & i
Next

Hope it help

Line 48 in your function IsPrime was wrong For i = 1 To number \ 2 see this :

Private Function IsPrime(ByVal number As Long) As Boolean
        Dim i As Integer
        If number = 1 Then
            Return False
        ElseIf number = 2 Then
            Return True
        ElseIf number > 2 Then
            For i = 2 To number - 2
                If number Mod i = 0 Then
                    Return False
                End If
            Next i
        End If
        Return True
    End Function

When you cheking prime number, line 28 & 29 is wrong.

For i = LowerBound To UpperBound
    If IsPrime(i) = True Then
    primeTextBox.Text = primeTextBox.Text & " " & i
Next

Hope it help

Thanks man. I really appreciate it.:) i would like to know how to list these numbers in a row.
Ex:1
1
1.. and so on. thanks again.

i would like to know how to list these numbers in a row.

you mean to make the results in textbox like a row?
ex :
2
3
5
7

Set textbox properties, Multiline = True
change this following part of your code :

For i = LowerBound To UpperBound
    If IsPrime(i) = True Then
      primeTextBox.Text = primeTextBox.Text & VbCrlf & i  ' Change space (" ") with Enter (VbCrlf)
    End If
Next

got it thanks

You're Welcome.
Don't forget to mark this thread as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.