CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Prime Numbers

  1. #1
    Join Date
    Jan 2000
    Location
    Ohio
    Posts
    4

    Prime Numbers

    I'm currently in a VB class at my high school. And the project that we are working on now is finding prime numbers. We must first start out with prime numbers that a less then 100, once we have found out how to do that we move on to 1,000... 10,000 and so on. Then we'll time our project and compare them to see who's program can find primes the fastest. What i am asking for is help on writing some code for this project. Hints, tips or if anyone wants bo be kind enough to tell me how to do the whole thing, that would be great. Thank You, Sincerely, doublej


  2. #2
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: Prime Numbers

    Well, I doubt you'll get your homework done by anyone here, but I will give you a hint on finding prime numbers:

    You only have to check prime numbers up to the square root of the number your testing. For example. If you are testing 91, you would start with 2, 3, 5, 7 and so on, using prime numbers to see if they divide evenly into the number in question (91). Obviously you only have to use primes instead of all the numbers, because all composite numbers are made up of primes. Anyway, take the square root of 91 and round it off to an integer, then just test primes that are less than or equal to that number. Anything greater than that would be too large. For example, if you were testing 87, you can guess that the square root isn't bigger than 9, because the 10 square is 100. so, you begin your testing. 2 - no, 3 - yes!( 3*29 = 87) done. what about 83? again, 2 - no, 3 - no, 5 - no, 7 - no, the next prime is 11 and that squared is 121 - too big. hence 83 must be prime.

    hope this helps,
    John

    ps. if you get the work done by someone else's hand, then you haven't learned anything. There is a great opportunity here to be very creative in your programming. take advantage of it.

    John Pirkey
    MCSD
    www.ShallowWaterSystems.com
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

  3. #3
    Join Date
    Dec 1999
    Location
    MLA, PHIL
    Posts
    11

    Re: Prime Numbers

    TRy this..modify the codes for faster iteration (if your going to use 10000 numbers and above)


    Private Sub computeprime(ByVal lngRange As Long)
    Dim i As Long, j As Long
    Dim blnPrime As Boolean
    Text1.Text = ""
    For i = 3 To lngRange
    For j = 2 To i - 1
    blnPrime = True
    If (i Mod j) = 0 Then
    blnPrime = False
    Exit For
    End If

    Next j
    ' for displaying the numbers
    If blnPrime Then
    Text1.Text = Text1.Text & i & ", "
    End If
    Next i
    End Sub

    Private Sub Command1_Click()
    Call computeprime(10000)
    End Sub



    Ricky

    [email protected]


  4. #4
    Guest

    Re: Prime Numbers

    there is a program called "Prime Base". It gets all the prime #'s between any two given numbers and checks if a number is prime. Really simple. If you want it, go to http://redsting71.bizland.com and go to the link 'PRIME BASE'. Download it there


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured