Search Tools Links Login

primechecker


Visual Basic 6, or VB Classic

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

Original Author: Max Christian Pohle

Inputs

a number

Returns

true or false

Code

'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

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 134 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.