CodeItBetter Programming Another VB Programming Blog

How to check whether given number is a Prime Number

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
'Math - How to check whether given number is a Prime Number
Option Explicit
 
Public Function IsPrime(ByVal n As Long) As Boolean
    Dim I As Long
    IsPrime = False
    If n <> 2 And (n And 1) = 0 Then Exit Function 'test if div 2
    If n <> 3 And n Mod 3 = 0 Then Exit Function 'test if div 3
    For I = 6 To Sqr(n) Step 6
        If n Mod (I - 1) = 0 Then Exit Function
        If n Mod (I + 1) = 0 Then Exit Function
    Next I
    IsPrime = True
End Function
Filed under: Math Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.