elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Como proteger una cartera - billetera de Bitcoin


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP)
| | | |-+  Programación Visual Basic (Moderadores: LeandroA, seba123neo)
| | | | |-+  [RETO] Project Euler 3
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: 1 [2] Ir Abajo Respuesta Imprimir
Autor Tema: [RETO] Project Euler 3  (Leído 6,393 veces)
BlackZeroX
Wiki

Desconectado Desconectado

Mensajes: 3.158


I'Love...!¡.


Ver Perfil WWW
Re: Re: [RETO] Proyect Euler 3
« Respuesta #10 en: 2 Febrero 2013, 05:28 am »

.
El perico es verde en cualquier parte.

@Psyke1
¿Cual trampa? nadie dijo que NO se podía hacer lo que hice...

Dulces Lunas!¡.


« Última modificación: 2 Febrero 2013, 05:36 am por BlackZeroX (Astaroth) » En línea

The Dark Shadow is my passion.
BlackZeroX
Wiki

Desconectado Desconectado

Mensajes: 3.158


I'Love...!¡.


Ver Perfil WWW
Re: Re: [RETO] Proyect Euler 3
« Respuesta #11 en: 2 Febrero 2013, 07:08 am »

Tal vez esta versión no sea rápida pero usa una generación de números primos que se deseaban...

Código
  1. Option Explicit
  2. Option Base 0
  3.  
  4. Dim ManagerPrime    As clsPrime
  5.  
  6. Private Sub Form_Load()
  7.    Set ManagerPrime = New clsPrime
  8.  
  9.    Call ManagerPrime.Clear
  10.    MsgBox ManagerPrime.ItsPrime(6857)
  11.    MsgBox "Para calcular si fue primo se calcularon " & ManagerPrime.CountPrime() & " numeros primos."
  12.    Call ManagerPrime.Clear
  13.  
  14.    ManagerPrime.BufferLimitIndex(7000) = &H7FFFFFFF
  15.    MsgBox ProyectEuler3_ByBlack
  16.  
  17.    Set ManagerPrime = Nothing
  18. End Sub
  19.  
  20. Public Function ProyectEuler3_ByBlack(Optional ByVal lNumber As Double = 600851475143#) As Double
  21. 'Dim ManagerPrime    As clsPrime
  22. Dim auxn            As Double
  23. Dim auxd            As Double
  24. Dim i               As Long
  25.    'Set ManagerPrime = New clsPrime
  26.    While lNumber > 1
  27.        DoEvents
  28.        auxd = lNumber / ManagerPrime.Prime(i)
  29.        auxn = Fix(auxd)
  30.        If (auxd = auxn) Then
  31.            lNumber = auxn
  32.        Else
  33.            i = (i + 1)
  34.        End If
  35.    Wend
  36.    ProyectEuler3_ByBlack = ManagerPrime.Prime(i)
  37.    'Set ManagerPrime = Nothing
  38. End Function
  39.  

clsPrime.cls
Código
  1. Option Explicit
  2. Option Base 0
  3.  
  4. Private m_N             As Long
  5. Private m_Now           As Long
  6. Private m_Arr()         As Long
  7. Const MAX_LIMIT_VALUE   As Long = &H7FFFFFFF
  8.  
  9. Private Sub Class_Initialize()
  10.    Clear
  11. End Sub
  12.  
  13. Private Function Calculate(ByVal Start As Long, Optional ByVal MaxLimitValue As Long = MAX_LIMIT_VALUE) As Long
  14. Dim j As Long
  15.  
  16.    For m_Now = Start To MaxLimitValue
  17.  
  18.        j = (m_Now - 1)
  19.  
  20.        Do While (m_Now Mod j <> 0)
  21.            j = (j - 1)
  22.        Loop
  23.  
  24.        If (j < 2) Then
  25.            Calculate = m_Now
  26.            Exit Function
  27.        End If
  28.    Next
  29.  
  30.    Calculate = &H80000000
  31.  
  32. End Function
  33.  
  34. Public Sub Clear()
  35.    m_N = 0
  36.    m_Now = 2
  37.    ReDim m_Arr(m_N)
  38.    m_Arr(m_N) = 2
  39. End Sub
  40.  
  41. Public Property Let BufferLimitIndex(Optional ByVal MaxLimitValue As Long = MAX_LIMIT_VALUE, ByVal n As Long)
  42. Dim lRes        As Long
  43. Dim lNow        As Long
  44.  
  45.    Do While (m_N < n)
  46.        lRes = Calculate(m_Now + 1, MaxLimitValue)
  47.        If (lRes And &H80000000) Then
  48.            Exit Do
  49.        Else
  50.            m_N = (m_N + 1)
  51.            ReDim Preserve m_Arr(m_N)
  52.            m_Arr(m_N) = lRes
  53.        End If
  54.    Loop
  55. End Property
  56.  
  57. Public Property Get CountPrime() As Long
  58.    CountPrime = (m_N + 1)
  59. End Property
  60.  
  61. Public Property Get BufferLimitIndex(Optional ByVal MaxLimitValue As Long = MAX_LIMIT_VALUE) As Long
  62.    BufferLimitIndex = m_N
  63. End Property
  64.  
  65. Public Function Prime(ByVal Index As Long, Optional ByVal MaxLimitValue As Long = MAX_LIMIT_VALUE) As Long
  66. Dim lRes        As Long
  67.    Prime = &H80000000
  68.    If Index > m_N Then
  69.        BufferLimitIndex(MaxLimitValue) = Index
  70.        If Not (Index = m_N) Then Exit Function
  71.    End If
  72.    Prime = m_Arr(Index)
  73. End Function
  74.  
  75. Public Function ItsPrime(ByVal Value As Long) As Boolean
  76. Dim i       As Long
  77. Dim lRes    As Long
  78.  
  79.    ItsPrime = True
  80.  
  81.    If (m_Now < Value) Then
  82.  
  83.        Do
  84.            lRes = Prime(m_N + 1, Value)
  85.            If (lRes And &H80000000) Then Exit Do
  86.        Loop While (m_Arr(m_N) < Value)
  87.  
  88.        If Not (m_Arr(m_N) = Value) Then ItsPrime = False
  89.  
  90.    Else
  91.        For i = 0 To m_N
  92.            If (m_Arr(i) = Value) Then Exit For
  93.        Next
  94.  
  95.        ItsPrime = False
  96.  
  97.    End If
  98.  
  99. End Function
  100.  
  101. Private Sub Class_Terminate()
  102.    Call Clear
  103. End Sub
  104.  

Dulces Lunas!¡.


« Última modificación: 2 Febrero 2013, 07:22 am por BlackZeroX (Astaroth) » En línea

The Dark Shadow is my passion.
Psyke1
Wiki

Desconectado Desconectado

Mensajes: 1.089



Ver Perfil WWW
Re: [RETO] Proyect Euler 3
« Respuesta #12 en: 2 Febrero 2013, 15:08 pm »

Bueno he hecho unos test, aquí está el código:

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. :silbar:

Voy posteando el siguiente. :)

DoEvents! :P
« Última modificación: 2 Febrero 2013, 17:32 pm por Psyke1 » En línea

imoen


Desconectado Desconectado

Mensajes: 1.589



Ver Perfil
Re: [RETO] Project Euler 3
« Respuesta #13 en: 3 Febrero 2013, 20:45 pm »

Y para que dais tantas vueltas?¿ jeje

Esta clar si no tiene que calcular nada , pq ya esta precargado pues poco tiene que hacer


bs imoen
En línea

Medion Akoya p6624
i-3 370
8 gigas DDR 3 RAM //750 hd 5400
gforce gt425 optimus XDD
Esta es mi casa, mi pueblo , o lo que queda de el aun asi lucharemos ... POR BENALIA....!!

srta imoen
Páginas: 1 [2] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Projecto Euler problema 12
Ejercicios
lDanny 5 5,168 Último mensaje 16 Octubre 2010, 04:33 am
por [L]ord [R]NA
[RETO] Project Euler 1 « 1 2 3 4 5 »
Programación Visual Basic
Psyke1 42 19,842 Último mensaje 26 Enero 2013, 11:20 am
por imoen
[RETO] Project Euler 2 « 1 2 3 »
Programación Visual Basic
Psyke1 23 9,969 Último mensaje 25 Enero 2013, 23:19 pm
por Danyfirex
[RETO] Project Euler 4 « 1 2 »
Programación Visual Basic
Psyke1 10 5,640 Último mensaje 4 Febrero 2013, 23:32 pm
por imoen
Ayuda con el calculo de Pi por la Serie de Euler
Programación C/C++
Rollingman216 3 1,981 Último mensaje 24 Agosto 2017, 04:09 am
por engel lex
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines