Gracias Raul!!
DoEvents¡!
DoEvents¡!
Esta sección te permite ver todos los mensajes escritos por este usuario. Ten en cuenta que sólo puedes ver los mensajes escritos en zonas a las que tienes acceso en este momento.
Mostrar Mensajes MenúCita de: Dessa en 13 Agosto 2010, 12:57 PM
Leandro, probando como dice Karcrack (que devuelva un Boolean ingresando un numero Long) es un "Misil", muy buena, no era que las matematicas no eran tu fuerte ?
Option Explicit
Private Declare Sub RtlMoveMemory Lib "Kernel32" (ByVal Destination As Any, ByVal Source As Any, ByVal length As Long)
Public Function Check_Lucky_Number3(ByVal lNumber As Long) As Boolean
Dim lTempArray() As Long
Dim NextElim As Long
Dim lArrayUBound As Long
Dim m As Long
Dim x As Long
If lNumber = 1 Or lNumber = 3 Then
GoTo IsLucky
ElseIf (lNumber > 1) And (lNumber Mod 2 <> 0) Then
m = 1
For x = 1 To lNumber Step 2
ReDim Preserve lTempArray(m)
lTempArray(m) = x
m = m + 1
Next
NextElim = 3: m = 2
Do
x = NextElim
Do While x <= UBound(lTempArray)
lArrayUBound = UBound(lTempArray)
If Not x = lArrayUBound Then
RtlMoveMemory VarPtr(lTempArray(x)), VarPtr(lTempArray(x + 1)), (lArrayUBound - x) * 4
ReDim Preserve lTempArray(lArrayUBound - 1)
Else
Exit Function
End If
x = x + (NextElim - 1)
Loop
m = m + 1
NextElim = lTempArray(m)
Loop While Not NextElim > lArrayUBound
IsLucky: Check_Lucky_Number3 = True
End If
End Function
CitarLeandroA IsLuckyNumber ---> 125
PsyKe1 Check_Lucky_Number3 ---> 125
Option Explicit
Function IsLucky2(lngNum As Long) As Boolean
Dim x As Long, cont As Long, contStep As Long, Indice As Long, numLuck() As String
If lngNum < 1 Or lngNum Mod 2 = 0 Or lngNum = 5 Then Exit Function
If lngNum = 1 Or lngNum = 3 Then IsLucky2 = True: Exit Function
For x = 1 To lngNum Step 2
ReDim Preserve numLuck(contStep)
numLuck(contStep) = x
contStep = contStep + 1
Next
contStep = 0: cont = 0: Indice = 1
While numLuck(Indice) <= UBound(numLuck)
For x = 0 To UBound(numLuck)
If cont = numLuck(Indice) - 1 Then
cont = 0
Else
numLuck(contStep) = numLuck(x)
cont = cont + 1
contStep = contStep + 1
End If
Next
If contStep = numLuck(Indice + 1) Then ReDim Preserve numLuck(contStep - 2) Else ReDim Preserve numLuck(contStep - 1)
If numLuck(UBound(numLuck)) <> lngNum Then Exit Function
cont = 0
contStep = 0
Indice = Indice + 1
Wend
IsLucky2 = True
End Function
If (lNumb And 1 = 0) Then Exit Function
If numLuck(UBound(numLuck)) = lngNum Then IsLucky = True
For x = 0 To UBound(numLuck)
If numLuck(x) = lngNum Then
IsLucky = True
Exit For
End If
Next
Cita de: simorg en 12 Agosto 2010, 00:17 AMNo se, ultimamente esta un poco callada, me niega el saludo, y no para de decirme que entre en el maldito Blog...
@*PsYkE1*
¿No estará Bianc4 de vacaciones?....jeje
P.D. Es broma
salu2.
Cita de: ssccaann43 en 12 Agosto 2010, 18:22 PMJajajajajaja
Jajajajaja...! *PsYkE1* te has vuelto un adicto al Collection...!
Excelente trabajo...! Me gustó..!
Cita de: Karcrack en 12 Agosto 2010, 23:53 PMGracias¡!
Mi codigo todavia no es funcional, tiene varios fallos, por ejemplo, en la segunda llamada da errores, debido a que dejo las variables llenas de basura... a ver si consigo mañana algo de tiempo y hago la version raaaapida
Buen trabajo Psyke, veo que has exprimido al maximo las neuronas, a mi me dejo con dolor de cabeza , tanto tiempo sin pensar...
Private tmr As CTiming
Option Explicit
Option Base 1
'Karcrack
Public Static Function IsItLucky(ByVal lNumb As Long) As Boolean
Dim bvSieve() As Byte
Dim lJump As Long
Dim lLastNumb As Long
Dim i As Long
Dim iCount As Long
Dim xCount As Long
Dim x As Long
If lNumb = 1 Or lNumb = 3 Then IsItLucky = True: Exit Function
If (lNumb And 1 = 0) Then Exit Function
If lJump = 0 Then lJump = 2
If lLastNumb < lNumb Then
ReDim Preserve bvSieve(lNumb)
iCount = 0
xCount = 1
Do
For i = 1 To lNumb
If bvSieve(i) = False Then iCount = iCount + 1
If iCount = lJump Then
bvSieve(i) = True
iCount = 0
End If
Next i
iCount = 0
xCount = xCount + 1
For i = 1 To lNumb
If bvSieve(i) = False Then
x = x + 1
If x = xCount Then
lJump = i
x = 0
Exit For
End If
End If
Next i
Loop Until xCount > lJump
End If
IsItLucky = Not bvSieve(lNumb)
lLastNumb = lNumb
End Function
'*PsYkE1*
Public Function Check_Lucky_Number(ByVal lNumber As Long) As Boolean
Dim cTemp As New Collection
Dim NextElim As Long
Dim m As Long
Dim x As Long
If lNumber = 1 Or lNumber = 3 Then
GoTo IsLucky
ElseIf (lNumber > 1) And (lNumber Mod 2 <> 0) Then
With cTemp
For x = 1 To lNumber Step 2
.Add x
Next
NextElim = 3: m = 2
Do
x = NextElim
Do While x <= .Count
.Remove (x)
x = x + (NextElim - 1)
Loop
If .Item(.Count) = lNumber Then
m = m + 1
NextElim = .Item(m)
Else
Exit Function
End If
Loop While Not NextElim > .Count
End With
IsLucky: Check_Lucky_Number = True
End If
End Function
' LeandroA
Private Function IsLuckyNumber(ByVal Num As Long) As Boolean
Dim lCount As Long, lPos As Long
Dim c As New Collection
If Num < 1 Then Exit Function
If Num Mod 2 = 0 Then Exit Function
For lPos = 1 To Num Step 2
c.Add lPos
Next
lCount = 1
Do While c.Count > lCount
lCount = lCount + 1
lPos = c(lCount)
Do
If lPos > c.Count Then Exit Do
c.Remove lPos
lPos = lPos + c(lCount) - 1
Loop
If c(c.Count) <> Num Then Exit Function
Loop
IsLuckyNumber = True
End Function
Private Sub Form_Load()
Dim x As Long
Dim sResult As String
Set tmr = New CTiming
tmr.Reset
For x = 1 To 500
If IsLuckyNumber(x) Then ' Aqui los voy probando uno a uno... :P
sResult = sResult & x & " "
End If
Next
MsgBox tmr.sElapsed
Debug.Print sResult
End Sub
CitarEl algoritmo se las trae!!Ya te digo, me costó bastante...
Option Explicit
Public Function Check_Lucky_Number(ByVal lNumber As Long) As Boolean
Dim cTemp As New Collection
Dim NextElim As Long
Dim m As Long
Dim x As Long
If lNumber = 1 Or lNumber = 3 Then
GoTo IsLucky
ElseIf (lNumber > 1) And (lNumber Mod 2 <> 0) Then
With cTemp
For x = 1 To lNumber Step 2
.Add x
Next
NextElim = 3 : m = 2
Do
x = NextElim
Do While x <= .Count
.Remove (x)
x = x + (NextElim - 1)
Loop
If .Item(.Count) = lNumber Then
m = m + 1
NextElim = .Item(m)
Else
Exit Function
End If
Loop While Not NextElim > .Count
End With
IsLucky: Check_Lucky_Number = True
End If
End Function
Option Explicit
Public Function Check_Lucky_Number2(ByVal lNumber As Long) As Boolean
Dim lTempArray() As Long
Dim NextElim As Long
Dim m As Long
Dim x As Long
If lNumber = 1 Or lNumber = 3 Then
GoTo IsLucky
ElseIf (lNumber > 1) And (lNumber Mod 2 <> 0) Then
m = 1
For x = 1 To lNumber Step 2
ReDim Preserve lTempArray(m)
lTempArray(m) = x
m = m + 1
Next
NextElim = 3 : m = 2
Do
x = NextElim
Do While x <= UBound(lTempArray)
Call Delete_Array_Item(lTempArray, x)
x = x + (NextElim - 1)
Loop
If lTempArray(UBound(lTempArray)) = lNumber Then
m = m + 1
NextElim = lTempArray(m)
Else
Exit Function
End If
Loop While Not NextElim > UBound(lTempArray)
IsLucky: Check_Lucky_Number2 = True
End If
End Function
' Esto lo hace MUY lento... :( Mirar sig version en la pág siguiente ;)
Private Sub Delete_Array_Item(ByRef lArray() As Long, ByVal lIndex As Long)
Dim lCount As Long
Dim x As Long
lCount = UBound(lArray)
If lIndex <= lCount And lIndex >= LBound(lArray) Then
For x = lIndex To lCount - 1
lArray(x) = lArray(x + 1)
Next
ReDim Preserve lArray(lCount - 1)
End If
End Sub
Private Sub Form_Load()
Dim x As Long
Dim sResult As String
For x = 1 To 200
'If Check_Lucky_Number2(x) Then
If Check_Lucky_Number(x) Then
sResult = sResult & x & " "
End If
Next
Debug.Print sResult
End Sub
Citar1 3 7 9 13 15 21 25 31 33 37 43 49 51 63 67 69 73 75 79 87 93 99 105 111 115 127 129 133 135 141 151 159 163 169 171 189 193 195