Jump to content

How to solve Project Euler problem 2 in Java?

- - - - -

  • Please log in to reply
7 replies to this topic

#1
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts
Hello forum, I have recently started to solve problems in a website called Project Euler.

I'm immensely confusing myself trying to solve these problems. The reason why I'm doing this is because, I would like to polish and better my math skills.

Here is problem_2.

Quote

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.

Here is my approach in java.
public static void main(String[] args)

{

    

int sum=0,prev1=2,prev2=4;

for(int i =0;prev1<4000000;i++){

    System.out.print("SUM:"+sum+"\n");

    sum= prev1+prev2; System.out.print("Previous: "+prev1+"\n");

    prev1=prev2;

    prev2=sum;

}

System.out.println("Result:"+ sum);

    

}


#2
Gikoskos

Gikoskos

    Newbie

  • Members
  • PipPip
  • 11 posts
So the program stops when the sequence reaches 4000000?

#3
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

Gikoskos said:

So the program stops when the sequence reaches 4000000?

No it stops when the term prev1 > 4000000

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Quote

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
I think they might want you to add the terms
2 + 8 = 10
8+34 = 42
34 + 144 = 178
etc...
And then add up all the bold terms.

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Looking at the terms: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
It wants you to add the even ones:
2+8+34+144+...
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

WingedPanther said:

Looking at the terms: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
It wants you to add the even ones:
2+8+34+144+...

Errr.. That's correct. I remember solving the problem a while back but I've lost the source to it.

#7
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts

WingedPanther said:

Looking at the terms: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
It wants you to add the even ones:
2+8+34+144+...

Would love to see how to code that to just add the even numbers!

#8
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
I actually just finished this problem in C# yesterday. I can try and why explain your Java code isn't working and also how to only add even numbers.
public static void main(String[] args)
 {     
    int sum=0, prev1=[COLOR=#ff0000]0[/COLOR], prev2=[COLOR=#ff0000]1[/COLOR];[COLOR=#008000] //Start from 0 and 1, instead of 2 and 4.[/COLOR]
[COLOR=#008000]   //I'm pretty sure you'll also need another variable to hold the total.[/COLOR]
    for(int i = 0; [COLOR=#ff0000]sum[/COLOR] < 4000000; i++) [COLOR=#008000]// We need to know when prev1+prev2 is above.[/COLOR]
    {
       System.out.print("SUM:"+sum+"\n");   
       sum = prev1+prev2;
       System.out.print("Previous: "+prev1+"\n");   
       prev1 = prev2;     
       prev2 = sum; 
     }
  System.out.println("Result:"+ sum);     
}

This shouldn't work until you make it only add even numbers. To do that you could throw in a if loop which would add prev1 and prev2 to a total variable(your using 'sum' to hold prev1+prev2 right now). Also you'll need to learn about the '%' operator for the if condition.
I hope you understand that, if you need any help just post back ~ Committed. :)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users