Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Project Euler Questio 2

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Project Euler Questio 2

    Hello, can any one please help me in problem 2 of project euler?

    Here is the problem::::::::::::


    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, ...

    By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.


    My Code:

    import java.io.*;

    class pro2
    {
    public static void main(String args[])
    {
    int a=1,b=2,c=0,sum=0;
    while(c<=4000000)
    {
    c=a+b;
    if((c%2)==0)
    {sum+=c;}
    a=b;
    b=c;
    }
    System.out.println(sum);

    }
    }


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Project Euler Questio 2

    Hello goldenpsycho!
    The only problem I can see with your code is that you start adding to sum the even prime numbers after three. i.e you don't add prime number 2 to your sum. Just add 2 to your sum and you will get what you want.
    Hope this helps.

  3. The Following User Says Thank You to andreas90 For This Useful Post:

    goldenpsycho (June 12th, 2012)

  4. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Project Euler Questio 2

    Hi andread90, thank you so much for your help. i can't believe that i overlooked such a small thing. I had written this program 3 times before coming to this conclusion so i guess i lost a bit of patience.

    Thanks, your solution worked.

Similar Threads

  1. Euler Paths with dfs algorithm problem
    By claudio.r in forum Algorithms & Recursion
    Replies: 18
    Last Post: March 22nd, 2012, 04:39 PM
  2. need help in my project
    By flana in forum The Cafe
    Replies: 4
    Last Post: October 11th, 2011, 06:13 AM
  3. [SOLVED] Project Euler: Truncatable Primes
    By Staticity in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 10th, 2011, 12:27 PM
  4. Euler integration
    By Kakashi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 2nd, 2009, 03:19 PM
  5. Project Euler
    By helloworld922 in forum The Cafe
    Replies: 5
    Last Post: September 9th, 2009, 02:51 PM