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 5 of 5

Thread: fibonnaci

  1. #1
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default fibonnaci

    I'm trying to calculate the following problem stuck at the moment, I don't want the answer rather would greatly appreciate if anyone could guide me to some resources which may help. I understand the problem just not sure how to represent it.

    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.

    I'm stuck on the part about the sum of even value terms. The following is my latest attempt which I know is wrong.
    public class main{
    	public static void main(String[] args){
     
    		for(int i = 0;i<=33;i++){
    			if(i%2==0){
     
    		System.out.print(fibonnaci(i) + " ");
    		}
    		}
    	}
    	static int fibonnaci(int n){
    		if(n==0){
    			return 0;
    		}else if(n==1){
    			return 1;
    		}else{
    			return fibonnaci(n-1)+ fibonnaci(n-2);
    	}
     
    	}
    }
    Thank you to the mod who advised me to check out project Euler, even though I'm rubbish at solving the problems, it's still great.
    Thank you for taking the time to read the thread.


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    606
    My Mood
    Sleepy
    Thanks
    10
    Thanked 91 Times in 84 Posts

    Default Re: fibonnaci

    You're too quick here. Break it up into little pieces. First try to gernerate the fibonacci series up to four million. When you've done that, think about how you add up the even vlues. Right now you're checkin whether the index of the sequence is even. I understand it that you have to add 2+8+34 and so on.

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

    keepStriving (May 14th, 2013)

  4. #3
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: fibonnaci

    Thank you for helping, I'll try again.

  5. #4
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: fibonnaci

    I've solved the question, I need to spend more time contemplating what the question is asking next time.

  6. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: fibonnaci

    Quote Originally Posted by keepStriving View Post
    I've solved the question...
    Please mark your threads as solved with the thread tools just above the first post.
    This is to save everyone time trying to help if you have no question left to be answered.

    Marking this thread solved for you