Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By JosAH

Thread: Fibonacci Sequence Problem

  1. #1
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Fibonacci Sequence Problem

    The challenge:
    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:
    Java Code:
    public class Problem2 {
    	public static void main (String args[]){
    		
    int fibonacci, fibonacci2, sum, evens;
    fibonacci=1;
    fibonacci2=1;
    sum=2;
    evens=0;
    
    	while (fibonacci<=4000000 || fibonacci2<=4000000 || sum<=4000000)
    		{
    		if (fibonacci<=4000000 && fibonacci%2==0){evens= evens + fibonacci;
    		}
    		if (fibonacci2<=4000000 && fibonacci2%2==0){
    			evens= evens + fibonacci2;
    		}
    		if (sum<=4000000 && sum%2==0){
    			evens= evens + sum;
    {sum=fibonacci + fibonacci2;
    fibonacci= sum + fibonacci2;
    fibonacci2= sum + fibonacci;
    	}
    System.out.println(fibonacci);
    System.out.println(fibonacci2);
    System.out.println(sum);
    
    	if (fibonacci>=4000000 ||
    fibonacci2>=4000000 ||
    sum>=4000000){
    		break;
    	}
    	}
    }
    
    System.out.println("Sum of the evens under 4 million: ");
    System.out.println(evens);
    	}
    }
    This is what it prints out:

    3
    5
    2
    13
    21
    8
    55
    89
    34
    233
    377
    144
    987
    1597
    610
    4181
    6765
    2584
    17711
    28657
    10946
    75025
    121393
    46368
    317811
    514229
    196418
    1346269
    2178309
    832040
    5702887
    9227465
    3524578
    Sum of the evens under 4 million:
    1089156
    Problems:

    The answer is completely wrong, my "evens" interger totally fails, although I don't know why. The fibonacci numbers are correct, but not in order, and it's giving me numbers over 4 million which I don't want. I'm trying to tell it (but obviously failing) at the beginning and end of the loop to stop if any number is equal or greater to 4 million. But, since I'm adding numbers together, 3.9 mil + 3.9 mil is gonna be more than 4 mil, but my code isn't going to stop that. Although how it gets a 9mil number I have no idea.

    What I want to do is for "evens" to check every number under 4 million to see if %2==0, and then add that number to evens. I'd prefer if numbers over 4 million weren't generated by fibonacci, fibonacci2 or sum as well.

    Cheers!
    Last edited by Zigster; 04-23-2012 at 05:49 AM.

  2. #2
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Fibonacci Sequence Problem

    Would an array be better for this? How can I put the numbers I've printed into an array? And is there some way to get Phi and Pi in java (So I can use the Fibonacci forumla)? Thanks!

  3. #3
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    40
    Rep Power
    0

    Default Re: Fibonacci Sequence Problem

    Ι use recursion to find fibonacci sequence. I wish this help you.
    Java Code:
    public class Main{
    	
    	public static void main(String[] args){
    
    		long N=0,sum=0,y; 
    
    		while(fib(N)<4000000){
    		
    			y=fib(N);
    
    			if(y%2==0){
    				sum+=y;
    				System.out.println("Even F"+N+"\t "+y);
    			}
    
    			else{
    				System.out.println("F"+N+"\t "+y);
    			}
    			
    			N++;
    		}
    
    		System.out.println("SUM :"+sum);
    	
    	}
    
    	public static long fib(long N){
    		
    		if(N==1){   //f1=1
    			return 1;
    		}
    
    		if(N==0){   //f0=0
    			return 0;
    		}
    		//fn=fn-1 + fn-2
    		return fib(N-1)+fib(N-2);
    	}
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default Re: Fibonacci Sequence Problem

    I don't understand all that juggling; no matter how you generate the Fibonacci numbers, somewhere in the body of a loop (where the current Fibonacci number < 4000000) I'd like to see something like this:

    Java Code:
    if (fib%2 == 0) // even?
       sum+= fib; // add it to the sum
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Fibonacci Sequence Problem

    Thanks guys. Yes my code is very awkward. I couldn't remember how to solve the sequence with a formula (been a fair while since I did this kind of maths back in high school). Brand new to coding and java.

    So is: fib(N-1)+fib(N-2) the formula? I still don't really understand how it's being resolved in Kosmos' code, mostly the fib method I don't get. And why do you use long instead of int? And when fib (or y) is in the main method, what exactly does it do? Executes the fib method?

    I tried to copy and paste kosmos's code into eclipse to try it, but I get this error: Error: Could not find or load main class Main?

    Cheers!

  6. #6
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    40
    Rep Power
    0

    Default Re: Fibonacci Sequence Problem

    I use vim and terminal to write and run code.
    My public class (top level class) is named Main, thus the source file should be named Main.java
    The source file must have the same name as the public class.

    The function fib() returns the Nth term. fib(3)=2, fib(4)=3
    The next term is the sum of the two previous terms.
    Therefore fib(5)=fib(4)+fib(3)=3+2=5 , fib(N)=fib(N-1)+fib(N-2)
    The function is recursive Java Recursion with examples

    Java Code:
    Even F0	 0
    F1	 1
    F2	 1
    Even F3	 2
    F4	 3
    F5	 5
    Even F6	 8
    F7	 13
    F8	 21
    Even F9	 34
    F10	 55
    F11	 89
    Even F12	 144
    F13	 233
    F14	 377
    Even F15	 610
    F16	 987
    F17	 1597
    Even F18	 2584
    F19	 4181
    F20	 6765
    Even F21	 10946
    F22	 17711
    F23	 28657
    Even F24	 46368
    F25	 75025
    F26	 121393
    Even F27	 196418
    F28	 317811
    F29	 514229
    Even F30	 832040
    F31	 1346269
    F32	 2178309
    Even F33	 3524578
    SUM :4613732

  7. #7
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Fibonacci Sequence Problem

    Thanks Kosmos, it works now! Your explanation is great, I understand what is happening! That is what I wanted to do, but didn't know how. I'm going to bank the knowledge and move on to the next challenge.

    Actually I went back and fixed up this code:

    Java Code:
    public class Problem2 {
    	public static void main (String args[]){
    		
    		int fibonacci, fibonacci2, sum, evens;
    		fibonacci=0;
    		fibonacci2=1;
    		sum=0;
    		evens=0;
    		System.out.println("Fibonacci Sequence, evens only:");
    			while (sum<4000000)
    			{
    				sum += fibonacci;
    				fibonacci = fibonacci2;
    				fibonacci2 = sum;
    				
    				if (sum%2==0)
    				{
    				System.out.println(sum);
    				evens+=sum;
    				}
    			}
    		System.out.println("Sum of the evens under 4 million: " + evens);
    	}
    }
    Last edited by Zigster; 04-24-2012 at 08:38 PM. Reason: removed answer to problem

  8. #8
    oxana is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Fibonacci Sequence Problem

    public class Fibonacci {
    public static void main (String[] args)
    int sum=0, b=0, c=1;

    for(int i = 0; i<=20; i++){
    sum = b+c;
    b=c+sum;
    c=sum+b;
    System.out.println(b+c+sum);

    }



    }
    }

  9. #9
    oxana is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Fibonacci Sequence Problem

    this is wrong

  10. #10
    oxana is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Fibonacci Sequence Problem

    //this works

    public class Fibonacci {
    public static void main (String[] args) {
    int sum=0, b=0, c=1;

    for(int i = 0; i<=200; i++){
    sum = b+c;
    b=c;
    c=sum;
    System.out.println(sum);
    }



    }
    }
    out put 1
    2
    3
    5
    8
    13
    21
    34
    55
    89
    144
    233
    377
    610
    987
    1597
    2584
    4181
    6765
    10946
    17711
    28657
    46368
    75025
    121393
    196418
    317811
    514229
    832040
    1346269
    2178309
    3524578
    5702887
    9227465
    14930352
    24157817
    39088169
    63245986
    102334155
    165580141
    267914296

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default Re: Fibonacci Sequence Problem

    Are you out of scribbling paper? Please, don't abuse this forum for it.

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2011, 03:16 AM
  2. Fibonacci sequence using iterative and recursive method
    By baumboards in forum New To Java
    Replies: 5
    Last Post: 03-02-2011, 06:57 PM
  3. Fibonacci sequence
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 6
    Last Post: 03-25-2010, 06:59 AM
  4. Fibonacci summation problem
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 12-01-2009, 06:28 PM
  5. help with fibonacci problem
    By thekrazykid in forum New To Java
    Replies: 4
    Last Post: 12-12-2008, 10:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •