Prime Numbers
A prime number is a whole number greater than 1 that has no positive divisors other than itself and 1
A whole number greater than 1 that is not prime is called a composite number.
A simple (but slow) method for determining prime numbers is called "trial division"
This consists of testing whether "n" is a multiple of any integer between 2 and SQRT(n)
Public Function PrimeNumbers(ByVal iMaxNumbers As Integer) As Variant
Dim myarray As Variant
Dim iprimecount As Integer
Dim number As Long
Dim divisor As Long
Dim maxdivisor As Long
Dim isprime As Boolean
ReDim myarray(1 to MaxNumber, 1 to 2)
iprimecount = 1
For number = 1 to iMaxNumber
Select Case number
Case Is < 2
isprime = False
Case Is = 2
isprime = True
myarray(iprimecount,1) = number
iprimecount = iprimecount + 1
Case Is > 2
isprime = True
If number Mod 2 = 0 Then
isprime = False
Else
maxdivisor = CLnd(Sqr(number))
divisor = 3
Do While (divisor <= maxdivisor)
If (number Mod divisor = 0) Then
isprime = False
Exit Do
End If
divisor = divisor + 1
Loop
End If
If (isprime = True) Then
myarray(ipriecount, 1) = number
iprimecount = iprimecount + 1
End If
End Select
If (isprime = True) Then
'MsgBox number & " is prime"
Else
'MsgBox number & " is not prime"
End If
Next number
PrimeNumbers = myarray
End Function
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext