primechecker
prime number || not? Interesting: The fastest possibility 2 find out is not the recursive one!
Inputs
a number
Returns
true or false
Rate primechecker
(3(3 Vote))
'1 : as recursive function
Function IsPrime(Num As Long, Optional Start As Long = 3) As Boolean
If Num Mod 2 <> 0 Then
If Num Mod Start <> 0 Then
If Start > Sqr(Num) Then _
IsPrime = True Else _
IsPrime = IsPrime(Num, Start + 2)
End If
End If
End Function
'2 : as standard-function
Function IsPrime(Num As Long) As Boolean
Dim L As Long
If Num Mod 2 <> 0 Then
For L = 3 To Sqr(Num) Step 2
If Num Mod L = 0 Then Exit For
Next L
If L > Sqr(Num) Then IsPrime = True
End If
End Function
'example how2 call
Sub StartAndWrite()
Do
Me.Tag = Val(Me.Tag) + 1
If IsPrime(Me.Tag) Then
Open App.Path & "\primes.dat" For Append As #1
Print #1, val(Me.Tag) & ",";
Close #1
DoEvents
End If
Loop
End Sub
primechecker Comments
No comments yet — be the first to post one!
Post a Comment