Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /homepages/11/d274290037/htdocs/serabe/wp-content/plugins/jetpack/_inc/lib/class.media-summary.php on line 77

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /homepages/11/d274290037/htdocs/serabe/wp-content/plugins/jetpack/_inc/lib/class.media-summary.php on line 87
Problema 2 | Conecto ergo sum.
Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /homepages/11/d274290037/htdocs/serabe/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/11/d274290037/htdocs/serabe/wp-includes/post-template.php on line 284

Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /homepages/11/d274290037/htdocs/serabe/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/11/d274290037/htdocs/serabe/wp-includes/post-template.php on line 284

Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /homepages/11/d274290037/htdocs/serabe/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/11/d274290037/htdocs/serabe/wp-includes/post-template.php on line 284

Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /homepages/11/d274290037/htdocs/serabe/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/11/d274290037/htdocs/serabe/wp-includes/post-template.php on line 284

Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /homepages/11/d274290037/htdocs/serabe/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

Problema 2


Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /homepages/11/d274290037/htdocs/serabe/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

Enunciado:
Suma todos los números pares de la sucesión de Fibonacci menores que un cuatro millón.

Primera solución:

La primera solución es muy simple. Hay un método que devuelve un array con todos los números de la sucesión de Fibonacci menores que un máximo max tomando como inicio un array de dos elementos arr.

def fib1(max,arr=[1,1])
while(arr.last < max) do arr << (arr.last+arr[arr.size-2]) end arr.pop arr end[/ruby] Después simplemente se eliminan los impares y se suman los que quedan. [ruby] def euler2a(max, ini=[1,1]) fib1(max,ini).delete_if{|x| x%2 == 1}.inject{|memo,obj| memo+obj} end puts euler2a(4e6)[/ruby] Segunda solución:

Esta solución es un poco mejor. Se va a hacer un nuevo método que calcule todos los números de la sucesión de Fibonacci menores que un máximo max pero que sólo almacene aquellos que pasen una condición que se le pasa como bloque:

def fib2(max,a=1,b=1)
arr = []
arr << a if yield(a) while(bTercera solución:

Esta tercera solución es una modificación directa de la segunda. En vez de almacenar los datos, se suman directamente.

def fib3(max,a=1,b=1)
res = 0
res += a if yield(a)
while(bActualización: Hay diferencias entre la página de PyEuler y la del Proyecto Euler. El problema ha sido actualizado para corresponderse con esta última.

Leave a Comment


Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /homepages/11/d274290037/htdocs/serabe/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /homepages/11/d274290037/htdocs/serabe/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524