El ejercicio que estoy intentando resolver consiste en hacer la suma de
Fibonacci que consiste en la adición de los dos términos anteriores.
Ejemplo para los 10 primeros términos: 1, 2, 3, 5, 8, 13, 21, 34, 55,
89...
El problema es que tiene que hacer la suma sin rebasar 4000000, que por
el momento esa parte si la he podido completar, lo que no se como hacer
y que quisiera que me pudieran ayudar o explicar como hacerlo, es que el
resultado consiste solamente en la suma de los resultados pero solo los
que son números pares y no se como hacerlo, el código con el que estuve
practicando e intentando es el siguiente:
a = 1
b = 2
stop = 3000000
(1...500).each do
s = a + b
a = b
b = s
break if a > stop
end
puts = a
Les Agradecería mucho su ayuda.
Gracias.
--
Posted via http://www.ruby-forum.com/.
¿Es esto correcto?
a = 1
b = 2
stop = 4000000
while a < stop - b
a, b = b, a + b
end
puts a
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
Este es uno de los ejercicios en el Projecto Euler
http://projecteuler.net/index.php?section=problems
Muy buenos para la práctica. Animo.
CT
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
> ...It would be interesting to see what ceilings (like 4_000_000) this fails
> for.
>
> cheers,
> Todd
>
>
Fernando,
El ejercicio del cual hablas es parte del web site: www.eulerorg.net, donde
se ofrece un largo numero de otros ejercicios interesantes. La siguiente is
mi solucion, aunque no la mejor, pero es otra manera.
Que te diviertas!
=begin
Each new term in the Fibonacci sequence is generated by adding the previous
two terms.
By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Find the sum of all the even-valued terms in the sequence which do not
exceed four million.
http://projecteuler.net
=end
MAXSUM = 4000000
def fibo(previous_fibo, current_fibo,new_fibo,total)
if new_fibo.remainder(2) == 0
if new_fibo <= MAXSUM
total = total + new_fibo
else
puts "Found it. It is: #{total}"
exit
end
end
new_fibo = previous_fibo + current_fibo
previous_fibo = current_fibo
current_fibo = new_fibo
fibo(previous_fibo, new_fibo, new_fibo, total)
end
fibo(1,2,2,0)
--
Ruby Student
When you do (max_inverted + 1) in the previous line, this works for 4_000_000.
> #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.
Revisiting old emails. Sorry for noise, but important to be concise.
Todd