VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



primechecker

by Max Christian Pohle (9 Submissions)
Category: Math/Dates
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (3 Votes)

prime number || not? Interesting: The fastest possibility 2 find out is not the recursive one!

Inputs
a number
Code Returns
true or false

Rate primechecker

'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

Download this snippet    Add to My Saved Code

primechecker Comments

No comments have been posted about primechecker. Why not be the first to post a comment about primechecker.

Post your comment

Subject:
Message:
0/1000 characters