Girl27
2011-12-22 11:44:21 UTC
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