Segundo Problema del Proyecto Euler

Problem 2

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.

Bueno el problema pensándolo así es bastante sencillo. Recuerdo que me enseñaron a utilizar la recursividad, pues aquí expongo mi método recursivo hecho en Java

 

public static long resultado(long a, long b, long sum) {
    if (b>=4000000)
        return sum;
    sum += (b%2==0) ? b : 0;
    return resultado(b,a+b,sum);
}

Solamente hay que llamarlo con

System.out.println(resultado(1l,1l,0l));

y listo esto imprimirá en la pantalla el resultado… smile_regular

Interesante como solo unas cuantas líneas puede procesar una serie infinita… smile_wink

Propongan sus algoritmos, para así aprender de todos…

Advertisements

1 Response to “Segundo Problema del Proyecto Euler”


  1. 1 Unknown 2008-09-23 at 7:52 AM

    Hi,Do you need advertising displays, screen advertisings, digital sign, digital signages and LCDs? Please go Here:www.amberdigital.com.hk(Amberdigital).we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.
    amberdigital Contact Us
    website:www.amberdigital.com.hk
    alibaba:amberdigital.en.alibaba.com[giaffhjaccabdg]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Blog Stats

  • 172,176 hits

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 18 other followers

May 2008
M T W T F S S
« Apr   Jun »
 1234
567891011
12131415161718
19202122232425
262728293031  

Grendon’s Twitter

MSP Ecuador’s Twitter

Error: Twitter did not respond. Please wait a few minutes and refresh this page.


%d bloggers like this: