Page 1 of 1

How to get a list of Prime Numbers Rate Topic: -----

#1 nofear217  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 14
  • View blog
  • 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

				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.

Is This A Good Question/Topic? 1
  • +

Replies To: How to get a list of Prime Numbers

#2 nritzau  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 eddycat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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! :)
Was This Post Helpful? 0
  • +
  • -

#4 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 126
  • View blog
  • 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! :smartass:
Was This Post Helpful? 0
  • +
  • -

#5 Bakunawa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-July 13

Posted 30 July 2013 - 06:47 AM

Thank you so much its very helpful :bananaman:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1