Results 1 to 9 of 9
Thread: Fibonacci Sequence
 05142015, 04:09 AM #1Member
 Join Date
 Feb 2015
 Posts
 67
 Rep Power
 0
Fibonacci Sequence
I am not sure what is wrong with my code? The code produces a negative number when I type in 4000000 into the fib method parameter.
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 static void fib(int k) { int result = 0; int[] array = new int[k]; array[0] = 1; array[1] = 2; for (int i = 2; i < k; i++) { array[i] = array[i  2] + array[i  1]; } for (int even: array) { System.out.print(even + " "); if (even % 2 == 0) { result += even; } } System.out.println(result); }
 05142015, 04:14 AM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 18
Re: Fibonacci Sequence
The code produces a negative number when I type in 4000000 into the fib method parameter.
[Edit] I've just read the question as given to you! You aren't supposed to sum every second of the first 4 million terms, you are supposed to sum the even valued terms whose values are less than 4 million. That's quite different and there won't be anything like 4 million of them. If you answer the questions above you will get some idea of how many there might be. Not that it matters much, you could use a for loop and "break" when the value of the Fibonacci term exceeds 4 million.Last edited by pbrockway2; 05142015 at 04:21 AM.
 05142015, 06:05 AM #3Member
 Join Date
 Feb 2015
 Posts
 67
 Rep Power
 0
Re: Fibonacci Sequence
But my math is correct. For small amount of numbers it works but not for large amounts. I even rewrote it to:
Java Code:public void fib(int k) { int sum = 0; int term1 = 1; int term2 = 2; int term3 = 0; int i = 2; while (i < k) { term3 = term1 + term2; term1 = term2; term2 = term3; i++; if (term2 % 2 == 0) { sum += term2; } } System.out.println(term3); System.out.println(sum + 2); }
 05142015, 06:46 AM #4Member
 Join Date
 May 2015
 Location
 Netherlands
 Posts
 39
 Rep Power
 0
Re: Fibonacci Sequence
if I understand the question correctly, aren't you supposed to add all the even valued terms as long as they are less than 4 million?
meaning that you don't need 4 million iterations of the loop but stop the loop once term 3 reaches a value higher than 4 million.
if that's not it, maybe try finding out at which point the loop starts giving weird results and use the debugger to figure out what goes wrong and why there.
(from what I can tell the code seems to be right if you just want it to do an assigned number of iterations, I'm not a pro though so not sure how much my opinion is worth :P )
 05142015, 07:08 AM #5Member
 Join Date
 Feb 2015
 Posts
 67
 Rep Power
 0
 05142015, 07:40 AM #6Member
 Join Date
 May 2015
 Location
 Netherlands
 Posts
 39
 Rep Power
 0
Re: Fibonacci Sequence
You're very welcome :)
I used to have this problem in math, they ask questions in such a way that it's easily misunderstood.
Hope you managed to complete the assignment :)
 05142015, 02:34 PM #7Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 14
Re: Fibonacci Sequence
Pbrockway2 told you the same thing earlier. He also told you that ints have a maximum value. To be more specific, signed values use the highorder bit as the sign bit. If it is set, the number is negative. This means you can increment a positive value until it becomes negative. Try this example:
Java Code:int a = Integer.MAX_VALUE10; for (int j = 0; j < 20; j++) { System.out.println(a++); }
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 05142015, 11:28 PM #8Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 18
Re: Fibonacci Sequence
I don't know whether it works with big numbers
Just for fun I wrote out the first dozen or so terms of the Fibonacci sequence and decided how many bits "wide" they were. As jim829 points out the leftmost bit is used for the sign but leaving that detail aside, the resulting sequence of "bit sizes" is bad news for any attempt to represent the Fibonacci sequence with primitive values. And the exercise does provide an answer to my questions.
 05152015, 07:41 AM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
Re: Fibonacci Sequence
Erlang is no magic: it either also uses a large stack for recursive functions (methods) or it can use a large memo for its memozation of those recursive functions. (for the recursive fibonacci function it uses n slots in its memo table where n is the largest parameter value of the function; but an ordinary stack uses n slots also).
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads

Fibonacci sequence using Dynamic Arrays
By MylesPollie in forum New To JavaReplies: 6Last Post: 02172014, 04:26 PM 
need some help doing loops with fibonacci sequence
By barqcider in forum New To JavaReplies: 6Last Post: 09252013, 11:03 PM 
Fibonacci sequence program that only prints the n'th digit in the sequence
By erichfx in forum New To JavaReplies: 1Last Post: 01232013, 06:06 PM 
Fibonacci Sequence Problem
By Zigster in forum New To JavaReplies: 10Last Post: 07062012, 09:54 PM 
Fibonacci sequence
By ŖàΫ ỏƒ Ңόρę in forum New To JavaReplies: 6Last Post: 03252010, 06:59 AM
Bookmarks