Advertisement
7_2009-2012 Math/ Dates #221303

primechecker

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

AI

Resumo por IA: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Código fonte
original-source
'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
Comentários originais (3)
Recuperado do Wayback Machine