Results 1 to 11 of 11
Thread: Fibonacci Sequence Problem
 04232012, 05:39 AM #1Member
 Join Date
 Apr 2012
 Posts
 74
 Rep Power
 0
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 evenvalued terms.
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); } }
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
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; 04232012 at 05:49 AM.
 04232012, 09:02 AM #2Member
 Join Date
 Apr 2012
 Posts
 74
 Rep Power
 0
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!
 04232012, 09:40 AM #3Member
 Join Date
 Apr 2012
 Posts
 44
 Rep Power
 0
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=fn1 + fn2 return fib(N1)+fib(N2); } }
 04232012, 10:51 AM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
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
JosBuild a wall around Donald Trump; I'll pay for it.
 04232012, 02:54 PM #5Member
 Join Date
 Apr 2012
 Posts
 74
 Rep Power
 0
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(N1)+fib(N2) 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!
 04232012, 04:34 PM #6Member
 Join Date
 Apr 2012
 Posts
 44
 Rep Power
 0
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(N1)+fib(N2)
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
 04242012, 03:17 AM #7Member
 Join Date
 Apr 2012
 Posts
 74
 Rep Power
 0
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; 04242012 at 08:38 PM. Reason: removed answer to problem
 07062012, 09:35 PM #8Member
 Join Date
 Jul 2012
 Posts
 3
 Rep Power
 0
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);
}
}
}
 07062012, 09:38 PM #9Member
 Join Date
 Jul 2012
 Posts
 3
 Rep Power
 0
Re: Fibonacci Sequence Problem
this is wrong
 07062012, 09:42 PM #10Member
 Join Date
 Jul 2012
 Posts
 3
 Rep Power
 0
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
 07062012, 09:54 PM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
Similar Threads

Using forloop and arrays to calculate factorials and fibonacci sequence
By baumboards in forum New To JavaReplies: 8Last Post: 03102011, 03:16 AM 
Fibonacci sequence using iterative and recursive method
By baumboards in forum New To JavaReplies: 5Last Post: 03022011, 06:57 PM 
Fibonacci sequence
By ŖàΫ ỏƒ Ңόρę in forum New To JavaReplies: 6Last Post: 03252010, 06:59 AM 
Fibonacci summation problem
By xcallmejudasx in forum New To JavaReplies: 3Last Post: 12012009, 06:28 PM 
help with fibonacci problem
By thekrazykid in forum New To JavaReplies: 4Last Post: 12122008, 10:41 PM
Bookmarks