# Finding a value in a variable

Page 1 of 1

## 3 Replies - 839 Views - Last Post: 26 September 2011 - 01:38 PMRate 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=248655&amp;s=30dfbf0929d8c90695471a6d2da22f61&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mattwagner

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 26-September 11

# Finding a value in a variable

Posted 26 September 2011 - 12:23 PM

Hi, I've got to make a small program for my college course that finds the 1001st Prime number. So far i've managed to be able to create this code :
``` Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Prime As String
For i As Integer = 0 To 100000000
If Primecheck(i) Then Prime = i
Next
MsgBox(Prime, 1001)
End Sub
Private Function Primecheck(ByVal N As Integer) As Boolean

For i As Integer = 2 To N - 1
If N Mod i = 0 Then Return False
Next
Return True
End Function
End Class

```

I was just wondering if there was any way to just display the 1001st value in the variable "Prime"?
If not would anyone be able to tell me of any other ways to achieve the 1001st prime number?

Many Thanks

Is This A Good Question/Topic? 0

## Replies To: Finding a value in a variable

### #2 modi123_1

• Suitor #2

Reputation: 13857
• Posts: 55,331
• Joined: 12-June 08

## Re: Finding a value in a variable

Posted 26 September 2011 - 12:42 PM

I do believe your for-loop (line 5) needs reconfiguring.

What you have there is:

for every number called 'i' from 0 to 100000000
if 'i' passes the "primecheck" then set the variable Prime to i.

When the forloop is done display a message box of that string value of Prime, and a message box style that is equivolent to 1001.

Primecheck returns false if the incoming value n mod any number between itself and 2 equals zero.

Something sounds off..

### #3 Psyguy

• D.I.C Regular

Reputation: 83
• Posts: 364
• Joined: 12-January 11

## Re: Finding a value in a variable

Posted 26 September 2011 - 01:31 PM

You should check out this link. I was doing a project a while back and found the brute force method took way too long. Implementing a "sieve" method to determine your primes works really well and is fairly easy to use. I was trying to find all primes under 2,000,000. The steps you see below in my program (still a hobby work in progress) are as follows:
1. filter out anything divisible by 2
2. go through the number list and perform a sieve on the number (filters out known non-primes)
3. go to the next number and sieve again, repeat until the end of the list is reached.

```Private Sub BGWcalculatePrimes_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BGWcalculatePrimes.DoWork

Dim input As Decimal 'finds all primes less than this number
Integer.TryParse(e.Argument, input) 'makes sure the input is an integer

setRemainingNumsText(input) 'just labels to show how many numbers are left while sieving
'fill list with non-even numbers up to the input and greater than 2.
For i = 3 To input
If i Mod 2 <> 0 Then
End If
Next
setRemainingNumsText(numberList.Count) 'not important
'Take out numbers which are multiples of lower level primes
For index = 0 To numberList.Count - 1
If index > numberList.Count - 1 OrElse numberList(numberList.Count - 1) < 2 * numberList(index) Then Exit For 'this is the check to make sure the index doesn't go out of the bounds of the size of the list as the sieve will remove numbers thus reducing the count
doSeive(numberList(index)) 'this is the main work horse
setRemainingNumsText(numberList.Count) 'not important
Next
'Add the number two to the list (the only even number that IS prime)
numberList.Insert(0, 2)

End Sub

Private Sub doSeive(ByVal n As Decimal)

Dim primes As New List(Of Decimal) 'create a new list to hold the filtered list of numbers
For num = 0 To numberList.Count - 1
If Not (n < numberList(num) AndAlso numberList(num) Mod n = 0) Then
End If
Next
numberList = primes 'replace the old number list with the filtered one...rinse/repeat

End Sub

```

Obviously, this isn't going to be super fast for large numbers, but it works.

### #4 modi123_1

• Suitor #2

Reputation: 13857
• Posts: 55,331
• Joined: 12-June 08

## Re: Finding a value in a variable

Posted 26 September 2011 - 01:38 PM

@Psyguy - I think your code overshoots the lesson of learning about loops and subroutines.