Question:
How to write a prime number function in visual basic.net?
Girl27
2011-12-22 11:44:21 UTC
Hey guys, I needed to make a program for finding prime numbers. The logic is that you have to put limits n & m and then LOOP and odd-integer P between n & m inclusive. I have a button to click on my form that displays 2 message boxes, one saying " enter upper limit" and the other say "enter lower limit". I need now to get the results of the odd prime numbers between the limits into a list box!
I am not sure how to write the prime testing function at the end of my code? Can someone help me find it out? I have everything else written.. Heres my code. Where am I going wrong?

"Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim str As String ' dont know why you want this
Dim n As Integer 'variable for the lower value
Dim m As Integer 'variable for the upper value

Dim TestLimit As String 'type all messed up
Dim TestPrime As String 'type all messed up
Dim TestNum As String 'type all messed

Dim x As Integer 'unnecessary as x is a local counter value but good practice

Dim isprime As Boolean ' no its the result of a function and it will be a boolean


str = "" 'why do you need this
n = InputBox("Enter lower limit") ' ok lets getvthe first value
m = InputBox("Enter Upper limit") ' ok lets get the next value

' good thats 2 value done, but no tests yet
' the actual output of the inboxes will be strings but the typing of n and m will cast them to integers


'***********************************************************
' the 2 tests your spec setout are

If n < 2 Then
msgbox("the lower limit is too small - start again")
Exit Sub
End If

If m > 50 Then
msgbox("the upperlimit is too big - start again")
Exit Sub
End If

'***********************************************************

'
' Eliminate even numbers
If n Mod 2 = 0 Then n = n + 1 'this ensures your lowerlimit is odd to start from

' Loop through ODD numbers starting with 3 ' what is the lowerlimit was 23

'***********************************************************
'junk the loop

TestNum = 3
TestLimit = TestPrime
Do While TestLimit > TestNum
For x = TestNum To TestLimit Step 2

Next
If TestPrime Mod TestNum = 0 Then
' Uncomment this if you really want to know
' MsgBox "Divisible by " & TestNum

Exit Sub
End If

' There's logic to this. Think about it.
TestLimit = TestPrime \ TestNum

' Remember, we only bother to check odd numbers
For x = n To m Step 2
'test x here to see if prime
'if x is prime with your function isprime() then add to list

If isprime(x) Then List.additem(x)
Next
Loop

' If we made it through the loop, the number is a prime.

Isprime = True

'end of junk
'*****************************************************************************

'*****************************************************************************
'replacement loop

' Remember, we only bother to check odd numbers

For x = n To m Step 2 ' this for does from an odd lowerlimit then +2,+4,+... up to the last odd number before the upperlimit

'test x here to see if prime
'if x is prime with your function isprime() then add to list

Next
If isprime(x) Then List.additem(x) 'if the result of a new function called isprime is true then store the x that was tested in the listbox
' loop some more


End Sub


'now you need to write the prime testing function

'I give you a start

Public Function isprime(ByVal arg) As Boolean

Dim n As Integer

isprime = True ' testing as long as true
Dim F As Integer
For F = 2 To n - 1
If n Mod F = 0 Then
isprime = False ' divisible
Exit For
End If
Next
If isprime = True Then ' Prime found

End If



End Function

End Class
Three answers:
texasmaverick
2011-12-23 05:52:51 UTC
Here is a code which will indicate in a listbox, the prime numbers in a span of numbers, I used StsrtNum and EndingNum instead of the n and m you used. It is better not to use lower case letters as variables.



I had a listbox, button and 2 textboxes on the form.







Public Class Form1

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

MsgBox("To find the prime numbers in a span of numbers, enter the starting number in text box1 and the ending number in text box 2.")

End Sub



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim StartNum As Integer = Val(TextBox1.Text)

Dim EndingNum As Integer = Val(TextBox2.Text)

Dim Chk1 As Integer



For i = StartNum To EndingNum

For j = 2 To i - 1

If i Mod j = 0 Then

Chk1 = 1

End If

Next

If Chk1 = 1 Then

Chk1 = 0

Else

ListBox1.Items.Add(i & " is a prime number.")

End If

Next

End Sub

End Class





TexMav
?
2011-12-22 12:12:33 UTC
Public Function isprime(byval inValue As Integer) As Boolean

Dim ReturnValue As Boolean = True

For F = 2 To inValue - 1

If inValue Mod F = 0 Then

ReturnValue = False

Exit For

End If

Next

Return ReturnValue

End Function



Now, you have a LOT of cleanup work to do on the rest of that code also. It is redundant and has unused variables, improperly defined items, etc.



Edited to add missing piece of code to FOR loop
2016-05-16 10:02:33 UTC
Me too. Do we win cookies?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...