Bueno he hecho unos test, aquí está el código:
Option Explicit
Private ManagerPrime As New clsPrime
Private Sub Form_Load()
Dim Q As Long
Dim t As New CTiming
Const LOOPS As Long = 500
If App.LogMode = 0 Then MsgBox "¡Compila!", vbCritical: End
Me.AutoRedraw = True
Me.Print "Con " & LOOPS & " vueltas"
Me.Print
t.Reset
Call ManagerPrime.Clear
ManagerPrime.BufferLimitIndex(7000) = &H7FFFFFFF
For Q = 1 To LOOPS
ProyectEuler3_ByBlack
Next Q
Me.Print "BlackZeroX -> ", t.sElapsed
t.Reset
For Q = 1 To LOOPS
ProyectEuler3_ByDark
Next Q
Me.Print "DarkMatrix -> ", t.sElapsed
t.Reset
For Q = 1 To LOOPS
PE3_DarkmodPsyke1
Next Q
Me.Print "DarkmodPsyke1 -> ", t.sElapsed
t.Reset
For Q = 1 To LOOPS
PE3_Psyke1
Next Q
Me.Print "Psyke1 -> ", , t.sElapsed
Set ManagerPrime = Nothing
End Sub
Public Function ProyectEuler3_ByBlack(Optional ByVal lNumber As Double = 600851475143#) As Double
'Dim ManagerPrime As clsPrime
Dim auxn As Double
Dim auxd As Double
Dim i As Long
'Set ManagerPrime = New clsPrime
While lNumber > 1
'DoEvents
auxd = lNumber / ManagerPrime.Prime(i)
auxn = Fix(auxd)
If (auxd = auxn) Then
lNumber = auxn
Else
i = (i + 1)
End If
Wend
ProyectEuler3_ByBlack = ManagerPrime.Prime(i)
'Set ManagerPrime = Nothing
End Function
Public Function ProyectEuler3_ByDark(Optional ByVal lNumber As Double = 600851475143#) As Double
Dim n As Double
Dim A As Double
Dim B As Double
Do
n = n + 1
A = lNumber / n
B = Fix(lNumber / n)
If A - B = 0 Then
lNumber = B
ProyectEuler3_ByDark = n
n = 1
End If
Loop Until lNumber = 1
End Function
Public Static Function PE3_DarkmodPsyke1(Optional ByVal dNumber As Double = 600851475143#) As Double
Dim lCount As Long
Dim dDiv As Double
Do While dNumber > 1
lCount = lCount + &H1
dDiv = dNumber / lCount
If dDiv = Fix(dDiv) Then
dNumber = dDiv
PE3_DarkmodPsyke1 = lCount
lCount = &H1
End If
Loop
End Function
Public Static Function PE3_Psyke1(Optional ByVal dNumber As Double = 600851475143#) As Double
Dim lCount As Long
Dim dDiv As Double
dDiv = dNumber / 2
If dDiv = Fix(dDiv) Then
dNumber = dDiv
End If
lCount = &H1
Do
lCount = lCount + &H2
dDiv = dNumber / lCount
If dDiv = Fix(dDiv) Then
dNumber = dDiv
dDiv = dNumber / 2
If dDiv = Fix(dDiv) Then
dNumber = dDiv
End If
PE3_Psyke1 = lCount
lCount = &H1
End If
Loop Until dNumber = 1
End Function
Y aquí unos resultados:
Obviamente a la larga gana la función de
BlackZeroX puesto que no tiene que calcular casi nada...
Yo personalmente me quedaría con la mía.
Voy posteando el siguiente.
DoEvents!