Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,611 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 914 people online right now. Registration is fast and FREE... Join Now!




How to get a list of Prime Numbers

 
Reply to this topicStart new topic

> How to get a list of Prime Numbers

nofear217
Group Icon



post 15 Jul, 2008 - 12:18 PM
Post #1


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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/23/08 02:33AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month