VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Returns true if the number is a prime number

by Waty Thierry (60 Submissions)
Category: Math/Dates
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Tue 13th April 1999
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Returns true if the number is a prime number

Rate Returns true if the number is a prime number



 Returns true if the number is a prime number.
 false if it is not.

 This should work reasonably well for small
 numbers (32-bits or less).  For larger numbers
 the Rabin-Miller test should be used.

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
   If n <> 5 And n Mod 5 = 0 Then Exit Function test if div 5
   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
   
   IsPrime = True

End Function



Download this snippet    Add to My Saved Code

Returns true if the number is a prime number Comments

No comments have been posted about Returns true if the number is a prime number. Why not be the first to post a comment about Returns true if the number is a prime number.

Post your comment

Subject:
Message:
0/1000 characters