Now I've seen this in various other languages and I thought I'd post one here for all your VB lovers. For some reason got it into my head to throw this together this afternoon so here we go:
This is assuming an input of two integers: a lower limit and an upper limit. And the code will capture any prime numbers between the two.
CODE
Dim lowerLimit, upperLimit, primeNumber As Integer
Dim isPrime As Boolean = False
lowerLimit = CType(TextBox1.Text, Integer)
upperLimit = CType(TextBox2.Text, Integer)
While lowerLimit < upperLimit
primeNumber = lowerLimit
If primeNumber = 2 Or primeNumber = 3 Then
isPrime = True
ElseIf primeNumber = 1 Then
isPrime = False
ElseIf primeNumber = upperLimit Then
Exit While
Else
For i As Integer = 2 To (primeNumber - 1)
If primeNumber Mod i = 0 Then
isPrime = False
Exit For
Else
isPrime = True
End If
Next
End If
If isPrime = True Then
TextBox3.Text += primeNumber & vbCrLf
End If
lowerLimit += 1
End While
As you can see, I used two textboxes for the lower and upper limit values and then when I found a prime number, I output it to a third multiline textbox. I skipped over 1-3 as one is of course not a prime number and then two and three do not fit well into the for loop with values. Which is why I started the for loop at 2 and looped up to the upperlimit minus 1. This is, of course, because a number is always divisible by 1 and itself (the definition of a prime number). If the loop ever catches a number that is divisble by another number with a 0 remainder it flags the number as non-prime and exits the loop. Otherwise it will continue looping through until it reaches one less than the upper limit.
Hope this helps someone out there.