3 Replies - 423 Views - Last Post: 26 September 2011 - 01:38 PM Rate Topic: -----

#1 mattwagner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9585
  • View blog
  • Posts: 36,323
  • 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..
Was This Post Helpful? 0
  • +
  • -

#3 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 76
  • View blog
  • Posts: 326
  • 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
                numberList.Add(i)
            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 
                primes.Add(numberList(num))
            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.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9585
  • View blog
  • Posts: 36,323
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1