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

IsPrime - Determine whether a number is prime
[ All Languages » VB »  Math]

Total Hit ( 1579)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Click here to copy the following block
' Return True if the number is prime

Function IsPrime(ByVal number As Long) As Boolean
  ' manually test 2 and 3
  If number > 3 Then
    If number Mod 2 = 0 Then Exit Function
    If number Mod 3 = 0 Then Exit Function
  End If
  ' we can now avoid to consider multiples
  ' of 2 and 3. This can be done really simply
  ' by starting at 5 and incrementing by 2 and 4
  ' alternatively, that is:
  '  5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, ...
  Dim divisor As Long
  Dim increment As Long
  Dim maxDivisor As Long
  
  divisor = 5
  increment = 2
  ' we don't need to go higher than the square
  ' root of the number
  maxDivisor = Sqr(number) + 1
  
  Do Until divisor > maxDivisor
    If number Mod divisor = 0 Then Exit Function
    divisor = divisor + increment
    ' this modifies 2 into 4 and viceversa
    increment = 6 - increment
  Loop
  
  ' if we get here, the number is prime
  IsPrime = True
  
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.