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

 

 


Tema destacado: Sigue las noticias más importantes de seguridad informática en el Twitter! de elhacker.NET


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

Mensajes: 760


www.leandroascierto.com


Ver Perfil WWW
Re: [RETO] Proyect Euler 1
« Respuesta #10 en: 23 Enero 2013, 20:38 pm »

Hola yo la verdad, no entiendo, en primer instancia dice que  3, 5, 6 and 9 son los que estan por devajo de 10, hasta hay todo bien, pero luego sus resultados no me son coherentes con esta lógica (aunque segun la pagina el resultado final es correcto)

pero por ejemplo el ejemplo de Danyfirex, solo mirando los primeros números de multiplos de 3 imprime esto
Citar
1             2             4             5             7             8             10            11

y no veo que el 2 sea un múltiplo de 3 , ni el resto de los siguientes.

ami la logica me dice algo asi,
Código
  1. Private Sub Form_Load()
  2.    Dim i As Long
  3.    Dim lSum As Long
  4.    Dim lResult As Long
  5.  
  6.    For i = 1 To 1000000
  7.        lResult = 3 * i
  8.        If lResult >= 1000 Then
  9.            Exit For
  10.        Else
  11.            lSum = lSum + lResult
  12.        End If
  13.  
  14.    Next
  15.  
  16.    For i = 1 To 1000000
  17.        lResult = 5 * i
  18.  
  19.        If lResult >= 1000 Then
  20.            Exit For
  21.        Else
  22.            lSum = lSum + lResult
  23.        End If
  24.  
  25.    Next
  26.  
  27.    Debug.Print lSum
  28. End Sub
  29.  

porque estoy equivocado???








En línea

Danyfirex


Desconectado Desconectado

Mensajes: 493


My Dear Mizuho


Ver Perfil
Re: [RETO] Proyect Euler 1
« Respuesta #11 en: 23 Enero 2013, 20:44 pm »

Hola yo la verdad, no entiendo, en primer instancia dice que  3, 5, 6 and 9 son los que estan por devajo de 10, hasta hay todo bien, pero luego sus resultados no me son coherentes con esta lógica (aunque segun la pagina el resultado final es correcto)

pero por ejemplo el ejemplo de Danyfirex, solo mirando los primeros números de multiplos de 3 imprime esto
y no veo que el 2 sea un múltiplo de 3 , ni el resto de los siguientes.

ami la logica me dice algo asi,


porque estoy equivocado???








hasta ahi vas bien. pero entonces te faltaría quitar los múltiplos de 15.


En línea

Psyke1
Wiki

Desconectado Desconectado

Mensajes: 1.089



Ver Perfil WWW
Re: [RETO] Proyect Euler 1
« Respuesta #12 en: 23 Enero 2013, 22:04 pm »

¡Jajajaja! ¡Menudo lío se ha montado! :laugh: Y eso que estamos con el reto 1. :silbar:

Creo que lo idóneo sería que le pudieramos pasar el número por parámetro, es más genérico.
Y la función debe funcionar SIEMPRE.

Aquí dejo unos ejemplos de llamadas que deben de devolver resultado correcto:
Código
  1. Debug.Print Euler1(1000) '-> 233168
  2. Debug.Print Euler1(0)    '-> 0
  3. Debug.Print Euler1(-983) '-> 0



Aquí dejo mi forma de hacerlo:
Código
  1. Public Static Function PE_1(ByVal lNum As Long) As Long
  2. Dim Q                           As Long
  3.  
  4.    If lNum And &H80000000 Then Exit Function
  5.  
  6.    lNum = lNum - 1
  7.  
  8.    For Q = 3 To lNum Step 3
  9.        PE_1 = PE_1 + Q
  10.    Next Q
  11.  
  12.    For Q = 5 To lNum Step 5
  13.        If Q Mod 3 Then
  14.            PE_1 = PE_1 + Q
  15.        End If
  16.    Next Q
  17. End Function

La próxima vez pondré el reto traducido, y el resultado que debe retornar para evitar confusiones. :rolleyes:

DoEvents! :P
« Última modificación: 23 Enero 2013, 22:18 pm por Psyke1 » En línea

Danyfirex


Desconectado Desconectado

Mensajes: 493


My Dear Mizuho


Ver Perfil
Re: [RETO] Proyect Euler 1
« Respuesta #13 en: 23 Enero 2013, 22:08 pm »

@Psyke1
creo la primera debería imprimir 234168 en vez de 233168.

100% de acuerdo en poner el reto en Español.
En línea

Psyke1
Wiki

Desconectado Desconectado

Mensajes: 1.089



Ver Perfil WWW
Re: [RETO] Proyect Euler 1
« Respuesta #14 en: 23 Enero 2013, 22:13 pm »

@Danyfirex

Código:
http://code.google.com/p/projecteuler-solutions/wiki/ProjectEulerSolutions

Ahí está el resultado de todas las soluciones. ;)

DoEvents! :P
En línea

Danyfirex


Desconectado Desconectado

Mensajes: 493


My Dear Mizuho


Ver Perfil
Re: [RETO] Proyect Euler 1
« Respuesta #15 en: 23 Enero 2013, 22:21 pm »

@Danyfirex

Código:
http://code.google.com/p/projecteuler-solutions/wiki/ProjectEulerSolutions

Ahí está el resultado de todas las soluciones. ;)

DoEvents! :P

A pues así pues si. bueno en fin aqui dejo la mia como la pides. no es lo mas optimo pero funciona bien.


Código
  1. Function mul_3_5(numero As Long) As Long
  2. Dim i As Integer
  3. For i = 0 To numero - 1
  4. If (i Mod 3) = 0 Or (i Mod 5) = 0 Then mul_3_5 = mul_3_5 + i
  5. Next i
  6. End Function



Cuando y donde podemos colocar el reto 2?
En línea

imoen


Desconectado Desconectado

Mensajes: 1.589



Ver Perfil
Re: [RETO] Proyect Euler 1
« Respuesta #16 en: 24 Enero 2013, 04:35 am »

En mi opinion este es el codigo mas claro

Al final pongo el visual basic eh xD

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
BlackZeroX
Wiki

Desconectado Desconectado

Mensajes: 3.158


I'Love...!¡.


Ver Perfil WWW
Re: [RETO] Proyect Euler 1
« Respuesta #17 en: 24 Enero 2013, 04:37 am »

mmm el código es fácil de hacer por lo cual yo solo me preocupe por la velocidad... evite calcular el modulo/residuo... ¿Alguien puede probar la velocidad?... se ve que la simplicidad de la función de 79137913 es más veloz.

Versión 1.
Código
  1. Public Function mul3and5(Optional ByVal dwBelowTo As Long = &H3E8&) As Long
  2. Dim i As Long
  3. Dim dwNewMax As Long
  4.  
  5.    If (dwBelowTo And &H80000000&) Then Exit Function
  6.  
  7.    dwNewMax = (dwBelowTo + &HFFFFFFFF&)
  8.    dwNewMax = (dwNewMax - (dwNewMax Mod &HF&))
  9.  
  10.    For i = &H5& To dwNewMax Step &HF&
  11.        mul3and5 = mul3and5 + i + i + &HFFFFFFFE&
  12.    Next
  13.    For i = &HA& To dwNewMax Step &HF&
  14.        mul3and5 = mul3and5 + i + i + i + &HFFFFFFFB&
  15.    Next
  16.    For i = &HF& To dwNewMax Step &HF&
  17.        mul3and5 = mul3and5 + i + i + &HFFFFFFFD&
  18.    Next
  19.  
  20.    i = (dwNewMax + &H3&):
  21.                    If (i >= dwBelowTo) Then Exit Function
  22.    mul3and5 = mul3and5 + i
  23.    i = (i + &H2&):    If (i >= dwBelowTo) Then Exit Function
  24.    mul3and5 = mul3and5 + i
  25.    i = (i + &H1&):    If (i >= dwBelowTo) Then Exit Function
  26.    mul3and5 = mul3and5 + i
  27.    i = (i + &H3&):    If (i >= dwBelowTo) Then Exit Function
  28.    mul3and5 = mul3and5 + i
  29.    i = (i + &H1&):    If (i >= dwBelowTo) Then Exit Function
  30.    mul3and5 = mul3and5 + i
  31.    i = (i + &H2&):    If (i >= dwBelowTo) Then Exit Function
  32.    mul3and5 = mul3and5 + i
  33. End Function
  34.  

Versión 2 (Menos iteraciones, más código).
Código
  1. Public Function mul3and5_Ver2(Optional ByVal dwBelowTo As Long = &H3E8&) As Long
  2. Dim i As Long
  3. Dim dwNewMax As Long
  4. Dim dwNewMaxFast As Long
  5.  
  6.    If (dwBelowTo And &H80000000) Then Exit Function
  7.  
  8.    dwNewMax = (dwBelowTo + &HFFFFFFFF)
  9.    dwNewMax = (dwNewMax - (dwNewMax Mod &HF&))
  10.  
  11.    If (dwNewMax > &H1E&) Then
  12.        dwNewMaxFast = (dwNewMax - (dwNewMax Mod &H1E&))
  13.        For i = &H5& To dwNewMaxFast Step &H1E&
  14.            mul3and5_Ver2 = mul3and5_Ver2 + i + i + i + i + &H1A&
  15.        Next
  16.        For i = &HA& To dwNewMaxFast Step &H1E&
  17.            mul3and5_Ver2 = mul3and5_Ver2 + i + i + i + i + i + i + &H23&
  18.        Next
  19.        For i = &HF& To dwNewMaxFast Step &H1E&
  20.            mul3and5_Ver2 = mul3and5_Ver2 + i + i + i + i + &H18&
  21.        Next
  22.    End If
  23.  
  24.    For i = dwNewMaxFast + &H5& To dwNewMax Step &HF&
  25.        mul3and5_Ver2 = mul3and5_Ver2 + i + i + &HFFFFFFFE&
  26.    Next
  27.    For i = dwNewMaxFast + &HA& To dwNewMax Step &HF&
  28.        mul3and5_Ver2 = mul3and5_Ver2 + i + i + i + &HFFFFFFFB
  29.    Next
  30.    For i = dwNewMaxFast + &HF& To dwNewMax Step &HF&
  31.        mul3and5_Ver2 = mul3and5_Ver2 + i + i + &HFFFFFFFD
  32.    Next
  33.  
  34.    i = (dwNewMax + &H3&):
  35.                    If (i >= dwBelowTo) Then Exit Function
  36.    mul3and5_Ver2 = mul3and5_Ver2 + i
  37.    i = (i + &H2&):    If (i >= dwBelowTo) Then Exit Function
  38.    mul3and5_Ver2 = mul3and5_Ver2 + i
  39.    i = (i + &H1&):    If (i >= dwBelowTo) Then Exit Function
  40.    mul3and5_Ver2 = mul3and5_Ver2 + i
  41.    i = (i + &H3&):    If (i >= dwBelowTo) Then Exit Function
  42.    mul3and5_Ver2 = mul3and5_Ver2 + i
  43.    i = (i + &H1&):    If (i >= dwBelowTo) Then Exit Function
  44.    mul3and5_Ver2 = mul3and5_Ver2 + i
  45.    i = (i + &H2&):    If (i >= dwBelowTo) Then Exit Function
  46.    mul3and5_Ver2 = mul3and5_Ver2 + i
  47. End Function
  48.  

P.D.: ¿Empezare a realizar el segundo... o me espero?.

Dulces Lunas!¡.
« Última modificación: 24 Enero 2013, 12:12 pm 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: [RETO] Proyect Euler 1
« Respuesta #18 en: 24 Enero 2013, 06:29 am »

Estos resultados fueron generados desde el IDE ya que no puedo compilar.
Código:
Tiempo 7913   978.902 msec
Resultado 7913              233168

Tiempo dany   1,647.315 msec
Resultado dany              233168

Tiempo Spyke1 488.101 msec
Resultado Spyke1            233168

Tiempo BlackZeroX V1        205.944 msec
Resultado BlackZeroX V1     233168

Tiempo BlackZeroX V2        125.884 msec
Resultado BlackZeroX V2     233168

CTiming.cls

Código:

Option Explicit
Option Base 0
 
Sub main()
Const LIM       As Long = 1000&
Const MAX_FOR   As Long = 100000
 
Dim i   As Long
Dim ct  As New CTiming
Dim obj As Object
 
    MsgBox "Empezara luego del Ok"
 
    ct.Reset
    For i = 1 To MAX_FOR
        mul3and5_Ver2 LIM
    Next
 
    Debug.Print "Tiempo BlackZeroX V2", ct.sElapsed
    Debug.Print "Resultado BlackZeroX V2", mul3and5_Ver2(LIM) & vbCrLf
 
    ct.Reset
    For i = 1 To MAX_FOR
        mul3and5 LIM
    Next
    Debug.Print "Tiempo BlackZeroX V1", ct.sElapsed
    Debug.Print "Resultado BlackZeroX V1", mul3and5(LIM) & vbCrLf
 
    ct.Reset
    For i = 1 To MAX_FOR
        mul5and3below1000
    Next
    Debug.Print "Tiempo 7913", ct.sElapsed
    Debug.Print "Resultado 7913", mul5and3below1000() & vbCrLf
 
    ct.Reset
    For i = 1 To MAX_FOR
        mul_3_5
    Next
    Debug.Print "Tiempo dany", ct.sElapsed
    Debug.Print "Resultado dany", mul_3_5() & vbCrLf
 
    ct.Reset
    For i = 1 To MAX_FOR
        PE_1 LIM
    Next
    Debug.Print "Tiempo Spyke1", ct.sElapsed
    Debug.Print "Resultado Spyke1", PE_1(LIM) & vbCrLf
 
End Sub
 
 
Private Function mul5and3below1000() As Long
    Dim ct As Long
    Dim aux As Long
    Dim aux2 As Long
    Do
        mul5and3below1000 = mul5and3below1000 + aux + aux2
        ct = ct + 1
        aux = ct + ct + ct
        aux2 = ct + ct + ct + ct + ct
    Loop While aux2 < 1000
    Do
        mul5and3below1000 = mul5and3below1000 + aux
        ct = ct + 1
        aux = ct + ct + ct
    Loop While aux < 1000
    ct = 0
    aux = 0
    Do
        mul5and3below1000 = mul5and3below1000 - aux
        ct = ct + 1
        aux = ct + ct + ct + ct + ct + ct + ct + ct + ct + ct + ct + ct + ct + ct + ct
    Loop While aux < 1000
End Function
 
Function mul_3_5() As Long
Dim i As Integer
For i = 1 To 999
If (i Mod 3) < 1 Or (i Mod 5) < 1 Then
mul_3_5 = mul_3_5 + i
End If
Next i
End Function
 
Public Static Function PE_1(ByVal lNum As Long) As Long
Dim Q                           As Long
 
    If lNum And &H80000000 Then Exit Function
 
    lNum = lNum - 1
 
    For Q = 3 To lNum Step 3
        PE_1 = PE_1 + Q
    Next Q
 
    For Q = 5 To lNum Step 5
        If Q Mod 3 Then PE_1 = PE_1 + Q
    Next Q
End Function
 
Public Function mul3and5(Optional ByVal dwBelowTo As Long = &H3E8&) As Long
Dim i As Long
Dim dwNewMax As Long
 
    If (dwBelowTo And &H80000000) Then Exit Function
 
    dwNewMax = (dwBelowTo + &HFFFFFFFF)
    dwNewMax = (dwNewMax - (dwNewMax Mod &HF&))
 
    For i = &H5& To dwNewMax Step &HF&
        mul3and5 = mul3and5 + i + i + &HFFFFFFFE
    Next
    For i = &HA& To dwNewMax Step &HF&
        mul3and5 = mul3and5 + i + i + i + &HFFFFFFFB
    Next
    For i = &HF& To dwNewMax Step &HF&
        mul3and5 = mul3and5 + i + i + &HFFFFFFFD
    Next
 
    i = (dwNewMax + &H3&):
                    If (i >= dwBelowTo) Then Exit Function
    mul3and5 = mul3and5 + i
    i = (i + &H2&):    If (i >= dwBelowTo) Then Exit Function
    mul3and5 = mul3and5 + i
    i = (i + &H1&):    If (i >= dwBelowTo) Then Exit Function
    mul3and5 = mul3and5 + i
    i = (i + &H3&):    If (i >= dwBelowTo) Then Exit Function
    mul3and5 = mul3and5 + i
    i = (i + &H1&):    If (i >= dwBelowTo) Then Exit Function
    mul3and5 = mul3and5 + i
    i = (i + &H2&):    If (i >= dwBelowTo) Then Exit Function
    mul3and5 = mul3and5 + i
End Function
 
Public Function mul3and5_Ver2(Optional ByVal dwBelowTo As Long = &H3E8&) As Long
Dim i As Long
Dim dwNewMax As Long
Dim dwNewMaxFast As Long
 
    If (dwBelowTo And &H80000000) Then Exit Function
 
    dwNewMax = (dwBelowTo + &HFFFFFFFF)
    dwNewMax = (dwNewMax - (dwNewMax Mod &HF&))
 
    If (dwNewMax > &H1E&) Then
        dwNewMaxFast = (dwNewMax - (dwNewMax Mod &H1E&))
        For i = &H5& To dwNewMaxFast Step &H1E&
            mul3and5_Ver2 = mul3and5_Ver2 + i + i + i + i + &H1A&
        Next
        For i = &HA& To dwNewMaxFast Step &H1E&
            mul3and5_Ver2 = mul3and5_Ver2 + i + i + i + i + i + i + &H23&
        Next
        For i = &HF& To dwNewMaxFast Step &H1E&
            mul3and5_Ver2 = mul3and5_Ver2 + i + i + i + i + &H18&
        Next
    End If
 
    For i = dwNewMaxFast + &H5& To dwNewMax Step &HF&
        mul3and5_Ver2 = mul3and5_Ver2 + i + i + &HFFFFFFFE
    Next
    For i = dwNewMaxFast + &HA& To dwNewMax Step &HF&
        mul3and5_Ver2 = mul3and5_Ver2 + i + i + i + &HFFFFFFFB
    Next
    For i = dwNewMaxFast + &HF& To dwNewMax Step &HF&
        mul3and5_Ver2 = mul3and5_Ver2 + i + i + &HFFFFFFFD
    Next
 
    i = (dwNewMax + &H3&):
                    If (i >= dwBelowTo) Then Exit Function
    mul3and5_Ver2 = mul3and5_Ver2 + i
    i = (i + &H2&):    If (i >= dwBelowTo) Then Exit Function
    mul3and5_Ver2 = mul3and5_Ver2 + i
    i = (i + &H1&):    If (i >= dwBelowTo) Then Exit Function
    mul3and5_Ver2 = mul3and5_Ver2 + i
    i = (i + &H3&):    If (i >= dwBelowTo) Then Exit Function
    mul3and5_Ver2 = mul3and5_Ver2 + i
    i = (i + &H1&):    If (i >= dwBelowTo) Then Exit Function
    mul3and5_Ver2 = mul3and5_Ver2 + i
    i = (i + &H2&):    If (i >= dwBelowTo) Then Exit Function
    mul3and5_Ver2 = mul3and5_Ver2 + i
End Function


Dulces Lunas!¡.
« Última modificación: 24 Enero 2013, 12:13 pm por BlackZeroX (Astaroth) » En línea

The Dark Shadow is my passion.
MCKSys Argentina
Moderador Global
***
Desconectado Desconectado

Mensajes: 5.465


Diviértete crackeando, que para eso estamos!


Ver Perfil
Re: [RETO] Proyect Euler 1
« Respuesta #19 en: 24 Enero 2013, 10:58 am »

Pongo el mío, aunque es más lento que el de BlackZeroX:

Código
  1. Public Function Euler_1(ByVal lNum As Long) As Long
  2. Dim Q As Long
  3. Dim S5 As Long
  4.  
  5.    If lNum& And &H80000000 Then Exit Function
  6.  
  7.    lNum& = lNum& - &H1
  8.  
  9.    For Q& = &H0 To (lNum& \ &HF)
  10.        Euler_1& = Euler_1& + (&HF * Q&)
  11.    Next Q
  12.  
  13.    S5& = 0
  14.    For Q& = &H1 To (lNum& \ &H5)
  15.        S5& = S5& + (&H5 * Q&)
  16.    Next Q
  17.    Euler_1& = S5& - Euler_1&
  18.  
  19.    For Q& = &H1 To (lNum& \ &H3)
  20.        Euler_1& = Euler_1& + (&H3 * Q&)
  21.    Next Q
  22. End Function
  23.  

En un EXE compilado, los tiempos me dan:

Código:
Tiempo BlackZeroX V2        55.732 msec
Resultado BlackZeroX V2     233168

Tiempo BlackZeroX V1        39.737 msec
Resultado BlackZeroX V1     233168

Tiempo 7913   152.260 msec
Resultado 7913              233168

Tiempo dany   1,627.764 msec
Resultado dany              233168

Tiempo Spyke1 175.199 msec
Resultado Spyke1            233168

Tiempo MCKSys 98.207 msec
Resultado MCKSys            233168

PD: Mirando el ASM generado, veo que el secreto está en no usar multiplicaciones ni divisiones. Aunque no es sencillo hallar un algoritmo con eso (y que sea diferente al de BlackZeroX!!  ;D)
En línea

MCKSys Argentina

"Si piensas que algo está bien sólo porque todo el mundo lo cree, no estás pensando."

Páginas: 1 [2] 3 4 5 Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Projecto Euler problema 12
Ejercicios
lDanny 5 5,131 Último mensaje 16 Octubre 2010, 04:33 am
por [L]ord [R]NA
[RETO] Project Euler 2 « 1 2 3 »
Programación Visual Basic
Psyke1 23 9,857 Último mensaje 25 Enero 2013, 23:19 pm
por Danyfirex
[RETO] Project Euler 3 « 1 2 »
Programación Visual Basic
Psyke1 13 6,320 Último mensaje 3 Febrero 2013, 20:45 pm
por imoen
[RETO] Project Euler 4 « 1 2 »
Programación Visual Basic
Psyke1 10 5,591 Ú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,951 Ú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