Page 1 of 1

## How to get a list of Prime Numbers Rate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=57649&amp;s=38c7a1149f9602337ff4bb87ecf6e0ac&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 nofear217

• D.I.C Regular

Reputation: 14
• Posts: 324
• Joined: 08-November 07

Posted 15 July 2008 - 01:18 PM

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.

```		Dim lowerLimit, upperLimit, primeNumber As Integer
Dim isPrime As Boolean = False

lowerLimit = CType(TextBox1.Text, Integer)
upperLimit = CType(TextBox2.Text, Integer)

While lowerLimit < upperLimit

isPrime = True
isPrime = False
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
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.

Is This A Good Question/Topic? 1

## Replies To: How to get a list of Prime Numbers

### #2 nritzau

Reputation: 4
• Posts: 66
• Joined: 31-January 09

Posted 20 February 2009 - 07:08 PM

Thanks that helped me

Moderator Note: There is no need to quote the entire post.
- Core

### #3 eddycat

Reputation: 0
• Posts: 1
• Joined: 26-February 09

Posted 26 February 2009 - 05:52 AM

do you know how to program a c++ code that will display the prime numbers in between two integers?thanks!

### #4 RetardedGenius

• ﻿>>──(Knee)──►

Reputation: 126
• Posts: 555
• Joined: 30-October 10

Posted 06 November 2010 - 01:51 PM

You could optimise this further by quite simply by checking for mod 2 then all of the odd integers between 2 and the square root of the number you are testing to see if it is prime.

You could even go one step further by creating a list with 2 as the first entry then test every odd integer from 3 using all of the primes you currently have up to the square root of the number you are testing. If that makes sense!