Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1529

Simple method for checking primality of number

$
0
0
This checks every positive integer between 2 and the Floor of the Squareroot of the number to be checked, to see if the Mod = 0. If it equals zero, then it is not prime. If it never is zero, then it is prime.

Code:

Private Function IsPrime(ByVal Value As Long) As Boolean
Dim Sqrt As Long
Dim n As Long

If Value < 2 Then Exit Function

Sqrt = Int(Sqr(Value))
For n = 2 To Sqrt
    If Value Mod n = 0 Then Exit For
Next n

If n = Sqrt + 1 Then IsPrime = True
End Function



On a related note, I made this below program that generates prime numbers from 2 to 10,000,000 (not including 10,000,000 as it is not prime). The way it works is this. At each number "n" it checks every previously detected prime number which has an index "i" (in the array the detected primes are stored in) between 2 and the Floor of the Squareroot of "n". It does this check via the Mod operator, and if the Mod is ever found to be 0 (zero) then that number is not prime and it immediately moves to the next number to be checked. It has a prime number checker in it similar to the prime checker above, but unlike the above program, the below one actually generates a list of prime numbers so it only needs to check the current number against the list of previously generated ones. The above prime number checker doesn't generate anything, so the number being checked needs to be checked against every number between 2 and the Floor of the Squareroot of the number being checked. The above PN checker is therefore less optimized in terms of the number of operations needed to check the primality of a given number, but unlike the below code, it doesn't have the burden of saving every found prime number. However the below code is a dedicated prime number generator so it can use the fact that it's generating prime numbers as a way of optimizing the checking of any future numbers for primality. The array of prime numbers it generates is saved to a text file.

Code:

Dim Primes() As Long



Private Sub Form_Load()
Dim n As Long
Dim i As Long
Dim NotPrime As Boolean
Dim Prime As Long
Dim Sqrt As Long

ReDim Primes(0)
Primes(0) = 2

For n = 3 To 10000000
    Sqrt = Int(Sqr(n))
    NotPrime = False
    For i = 0 To UBound(Primes)
        Prime = Primes(i)
        If Prime > Sqrt Then Exit For
        If n Mod Prime = 0 Then
            NotPrime = True
            Exit For
        End If
    Next i
    If NotPrime = False Then Push n
Next n

Open App.Path & "\primes.txt" For Output As #1
For i = 0 To UBound(Primes)
    Print #1, CStr(Primes(i))
Next i
Close #1
End Sub



Private Sub Push(ByVal Value As Long)
ReDim Preserve Primes(UBound(Primes) + 1)
Primes(UBound(Primes)) = Value
End Sub


Viewing all articles
Browse latest Browse all 1529

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>