-
February 25th, 2010, 07:22 PM
#1
Simple Program for Prime numbers...stumped
Hey Everyone so basically what im making is a form with just one button and 2 text boxes.
The purpose of it is that when i click the button 2 random PRIME numbers between 100-999 will show up in the boxes.
With my code i have now, the numbers that show up in the boxes arnt always PRIME and i cant figure out how to do this.
My current Code is.......
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Randomize()
Dim p, q, i, j As Integer
Dim IsPrime As Boolean
For p = 100 To 999
IsPrime = True
For i = 2 To Int(Math.Sqrt(p))
TextBox1.Text = CStr(Int(Rnd() * p))
If p Mod i = 0 Then
IsPrime = False
End If
Next
Next
If IsPrime Then
TextBox1.Text = p
End If
For q = 100 To 999
IsPrime = True
For j = 2 To Int(Math.Sqrt(q))
TextBox2.Text = CStr(Int(Rnd() * q))
If q Mod j = 0 Then
IsPrime = False
End If
Next
If IsPrime Then
TextBox2.Text = q
End If
Next
End Sub
End Class
ANy help is greatly appreciated
thanks
Last edited by Cimperiali; February 27th, 2010 at 04:30 PM.
Reason: added code tags and some indentation to code...
-
February 25th, 2010, 07:49 PM
#2
Re: Simple Program for Prime numbers...stumped
First, that looks like VB6 code. Second, use CODE TAGS so we can read your indentations
Lastly, looks like two NEXT's with one FOR
-
February 26th, 2010, 07:52 AM
#3
Re: Simple Program for Prime numbers...stumped
here a way to make it.
in a Class named PrimeNumbers
Code:
Public Class PrimeNumbers
Public Shared Function GetARandomPrimeNumber(ByVal lowerLimit As Integer, ByVal upperLimit As Integer) As Integer
Dim tikcs As Integer = My.Computer.Clock.TickCount
Dim rd As New Random(tikcs)
Debug.Print(tikcs.ToString())
Dim retval As Integer = rd.Next(lowerLimit, upperLimit)
Do
If IsPrime(retval) Then Exit Do
If retval < upperLimit Then
retval = retval + 1
Else
retval = rd.Next(lowerLimit, upperLimit)
End If
Loop
Return retval
End Function
Public Shared Function IsPrime(ByVal theNumber As Integer) As Boolean
'this function came from
'http://www.devx.com/vb2themax/Tip/19051
'ever read a book of Francesco Balena, Marco Bellinaso, Dino Esposito,...?
' manually test 2 and 3
If theNumber > 3 Then
If theNumber Mod 2 = 0 Then Exit Function
If theNumber Mod 3 = 0 Then Exit Function
End If
' we can now avoid to consider multiples
' of 2 and 3. This can be done really simply
' by starting at 5 and incrementing by 2 and 4
' alternatively, that is:
' 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, ...
Dim divisor As Integer
Dim increment As Integer
Dim maxDivisor As Integer
divisor = 5
increment = 2
' we don't need to go higher than the square
' root of the number
maxDivisor = CInt(Math.Sqrt(theNumber)) + 1
Do Until divisor > maxDivisor
If theNumber Mod divisor = 0 Then Exit Function
divisor = divisor + increment
' this modifies 2 into 4 and viceversa
increment = 6 - increment
Loop
' if we get here, the number is prime
Return True
End Function
End Class
in a form, with a label named lblInfo
a textBox named txtPrimeOne
a textbox named txtPrimeTwo
a button named btnGetPrime
Code:
Public Class frmPrime
Private Sub frmPrime_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
lblInfo.Text = "push button and get two random prime numbers between 100 and 999. Note: I did not make this smart enough to give you ALWAYS two different numbers (= you could get 101 as first and as second prime number, but I added some logic to avoid it happeniong too often...)"
End Sub
Private Sub btnGetPrime_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetPrime.Click
txtPrimeOne.Text = PrimeNumbers.GetARandomPrimeNumber(100, 999)
'need to pause at least 20 millisecond, or you will
'be likely get same numbers...
System.Threading.Thread.Sleep(25)
txtPrimeTwo.Text = PrimeNumbers.GetARandomPrimeNumber(100, 999)
End Sub
End Class
now prepare yourself to teacher questions:
what is "Shared" for?
What is "Math" ? What is "My"?
the way to check if a numer is prime is retrieved from
Euclid's theorem. Can you show it to us?
...and so on...
Last edited by Cimperiali; February 27th, 2010 at 04:26 PM.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
February 26th, 2010, 06:50 PM
#4
Re: Simple Program for Prime numbers...stumped
Definitely going to stand out in the class... You *will* need to explain it (and googling it will return to this site...)
' 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, ...
-
February 27th, 2010, 04:18 PM
#5
Re: Simple Program for Prime numbers...stumped
by the way...
the correct "good developer" way to get some numbers when required, when the data basis is few (from 100 to 999 it is few) is: prepare all the possible answers, then choose the two required randomly.
Ie:
load an array of prime numbers from 101 to 997 (100 and 998 and 999 are not prime numbers), only once. Then, on any request, choose two random numbers from that array (that is: choose a random position in the array twice and get the number stored; it is better than choose a random number between 100 and 999 and decide after if it is prime, and discard it if not...)
Last edited by Cimperiali; February 27th, 2010 at 04:34 PM.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|