Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

GetPrimeNumbers - Evaluate the first N prime numbers
[ All Languages » VB »  Math]

Total Hit ( 1577)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Click here to copy the following block
' Returns an array with the first N prime numbers
'
' Note: you can easily convert this routine to VB4 and VB5 by
' returning the result array through an argument instead of
' the return value

Function GetPrimeNumbers(numberOfPrimes As Long) As Long()
  Dim found As Long
  Dim n As Long
  Dim i As Long
  
  If numberOfPrimes <= 0 Then Err.Raise 1002, , "Invalid argument"
  
  ' we know the size of the result in advance
  ReDim result(1 To numberOfPrimes) As Long
  
  ' "2" is the first prime number
  result(1) = 2: found = 1
  
  n = 1
  Do
    ' all other prime numbers are odd, so we can skip even numbers
    n = n + 2
    ' let's check if N is a prime number
    For i = 1 To found
      If (n Mod result(i)) = 0 Then Exit For
    Next
    If i > found Then
      ' no prime number < N is a divisor for N
      ' therefore N is prime
      found = found + 1
      result(found) = n
      If found = numberOfPrimes Then Exit Do
    End If
  Loop
  
  GetPrimeNumbers = result
End Function


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.