From: Todd Benson Date: 2008-11-23T17:26:20+09:00 Subject: Re: Ayuda con Ejercicio On Thu, Nov 20, 2008 at 11:21 AM, wrote: > On Nov 19, 3:53 pm, Fernando Villafuerte > wrote: >> lo que no se como hacer... es que el >> resultado consiste solamente en la suma de los resultados >> pero solo los que son números pares... > > Solo te hace falta agregar una variable para la suma, incrementandola > cuando el numero es par (al dividirlo en 2 el residuo es cero): > >> a = 1 >> b = 2 >> stop = 4_000_000 > > suma = 0 > >> (1...500).each do >> s = a + b >> a = b >> b = s > > suma = suma + b if 0 == b.modulo(2) > >> break if a > stop >> end > > puts = suma For fun (no guarantee of accuracy, but works for 4_000_000)... include Math R = Math.sqrt(5) #radical P = (1 + R) / 2 #phi my_number = 4_000_000 #inverting my_number to see approximately where n sits for F(n) max_inverted = (log((my_number - 0.5) * R) / log(P)).to_i #adding up F(n) for 0 to n even_sum = (0..max_inverted).inject do |s, i| #next line is for direct F(n) #the inversion above is the opposite of this fib = (((P**i) - (1-P)**i) / R).floor #see if it's even fib % 2 == 0 ? (s + fib) : s end p even_sum ...It would be interesting to see what ceilings (like 4_000_000) this fails for. cheers, Todd