Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-25-2009, 06:15 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
Default Fibonacci summation problem
Hello all,

I'm slowly working my way through project euler and I'm having trouble with this question.

/* 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.
*/

Here is my code. The answers I've received from different code tweaks have been.
19544084, using the current code
82790068, using repeated terms(when term2 becomes term1 on it's next iteration)

Code:
public class Problem2 {

	/*  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.
	 */
	public static int term3 = 0;
	public static int sum = 0;
	public static void main(String[] args) {
		int term1 = 1, term2 = 2;
		System.out.println(fib(term1, term2));
	}
	
	public static int fib(int term1, int term2){
		if(term2 > 40000000){
			//System.out.println("sum: "+sum+", a: "+term1+", b: "+term2);
			return sum;
		}else{
			term3 = term1+term2;
			System.out.println("a: "+term1+", b: "+term2);
			if(term2 % 2 == 0){
				System.out.print("Adding "+term2+" to "+sum+" = ");
				sum += term2;
				System.out.print(sum+"\n");
			}
			return fib(term2, term3);
		}
	}
}
Can someone tell me where I might be going wrong? Or a more efficient way to do this. I know I'm doing extra computations since term2 ends up as term1 on the next call. I've noticed that an even answer only appears every 3rd call but I don't know how to incorporate this into some optimization. Any help would be appreciated.
__________________
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-25-2009, 06:28 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
Default
Wow. So I was using 40 million instead of 4 million, so disregard pretty much everything except the optimizations. Any advice on a more efficient algorithm?
__________________
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-25-2009, 10:06 PM
Senior Member
 
Join Date: Feb 2009
Posts: 589
Rep Power: 1
pbrockway2 is on a distinguished road
Default
My first thought would be to just use a for loop and accumulate the sum of the even valued terms. I wouldn't test for evenness since it's clear right from the start which ones are even...

Code:
public class FibSum {
    public static void main(String[] args) {
        int o = 1;
        int e = 2;

        int sum = 0;

        while(e <= 40000000) {
            // o e o+e o+2*e 2*o+3*e
            //     odd odd   even
            sum += e;
            /*
            int newO = o + 2 * e;
            int newE = 2 * o + 3 * e;
            o = newO;
            e = newE;
            */
            o += 2 * e;
            e = 2 * o - e;
        }
        System.out.println(sum); // 40M limit gives 19544084
    }
}
I'm not familiar with the "ethos" of project Euler. Are you supposed to use clever calculating or mathematical insight? Or both? I'd be looking for a "closed form" for the subsequence of even terms.

Last edited by pbrockway2; 11-25-2009 at 10:14 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-01-2009, 07:28 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
Default
Ya Project Euler has alot of math related questions for people to solve either using upper level mathematics or programming techniques. Your code is great. Aside from removing the recursive overhead in mine the only other way I thought to improve it would be a loop that doesn't bother to calculate if the number is even(thus removing a modulus calculation) and just added every third term, but your's essentially removed the need for a counter also. Very nice.
__________________
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with fibonacci problem thekrazykid New To Java 4 12-12-2008 11:41 PM
(Help) Quotient summation with prime numbers SapphireSpark New To Java 27 10-24-2008 09:28 AM
(Help) Fraction Summation and Exponents SapphireSpark New To Java 19 10-09-2008 05:01 AM
Printing Fibonacci Numbers Java Tip java.lang 0 04-09-2008 07:43 PM
Fibonacci Algorithm susan New To Java 1 08-07-2007 05:25 AM


All times are GMT +2. The time now is 07:04 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org