Results 1 to 18 of 18
- 01-05-2011, 02:22 AM #1
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Fibbonaci sequence without arrays
Im running into a problem creating this.
So far I can get the code to take a command line argument representing length of the sequence and print that many items
prints 5 numbers.Java Code:java Fibb 5
I can't get it to print anything except 1,1,1,1,1.
Im not looking for an answer, just for someone who is more experienced to look at my code.
My thought for making the code was to use a for loop, looping until cmd arg is met.
if i is 0, or 1, print 1,.
if ta or tb = 0, set them to 1, set another int, named num to ta + tb and print num,
Finally, set tmp to ta, set ta to ta + tb, set tb to tmp, and set num to ta + tb, printing num
The thought is, if i is 3, ta and tb will be set at 1.
so tmp will become 1, then ta will become 2, and tb will become 1, num will be 3.
i = 4:
tmp = 2, ta = 3, tb = 2, num = 5
i=5:
tmp = 3, ta = 5, tb =3, num = 8.
it seems to work, but my code is just spitting out 1's over and over. The only clause that produced print 1 is when i == 0 or i == 1.
heres the code:
Java Code:import static net.mindview.util.Print.*; import java.util.*; public class Fibb { public static void main(String[] args) { int tmp = 0; int ta = 0; int tb = 0; int num = 0; int items = Integer.parseInt(args[0]); for(int i = 0; i < items; i++) { if(i == 0 || i == 1) { System.out.print(1 + ", "); continue; } if(ta == 0 || tb == 0) { ta = 1; ta = 1; num = ta + tb; System.out.print(num + ", "); continue; } else { tmp = ta; ta = ta + tb; tb = tmp; num = ta + tb; System.out.print(num + ", "); continue; } } } }
- 01-05-2011, 02:58 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
I really dont understand what you are trying to do.
I had seen some but not sure if it is a logical error or what,
Good Luck! :)Java Code:import static net.mindview.util.Print.*; [b]//why is it STATIC?[/b] import java.util.*; public class Fibb { public static void main(String[] args) { int tmp = 0; int ta = 0; int tb = 0; int num = 0; int items = Integer.parseInt(args[0]); [b]//why are you using args?[/b] for(int i = 0; i < items; i++) { if(i == 0 || i == 1) { System.out.print(1 + ", "); continue; } if(ta == 0 || tb == 0) { ta = 1; [b]//you give value to ta[/b] ta = 1; [b]//AND you give value to ta AGAIN[/b] num = ta + tb; System.out.print(num + ", "); continue; } else { tmp = ta; ta = ta + tb; tb = tmp; num = ta + tb; System.out.print(num + ", "); continue; } } } }
- 01-05-2011, 03:19 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I actually just managed to fix it, the static import I am using Im really not sure why it's static, it was supplied by the author for ease early on in the book.
The idea of the function was to be able to specify how many items it wanted to go to
sowould produce the first 5 fibbonaci numbers.Java Code:java Fibb 5
- 01-05-2011, 05:13 AM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Correct me if I am wrong, Fibonacci numbers is a sequence of numbers that each subsequent is the sum of the previous two.
If so, I think there are some problem in your if..else statement.
Please post your latest code.I actually just managed to fix it, the static import I am using Im really not sure why it's static, it was supplied by the author for ease early on in the book.
Are you still using args[]?
Have you tried to run it?
EDT: My mistake, if..else statement is not a logical error, you just have to change what I have pointed out on my first reply.Last edited by mine0926; 01-05-2011 at 05:20 AM.
- 01-05-2011, 05:39 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
completed working code:
The bit with print("sum is " + sum); was something I was trying to do on project euler.Java Code:import static net.mindview.util.Print.*; import java.util.*; public class Fibb { public static void main(String[] args) { int tmp = 0; int ta = 1; int tb = 1; int num = 0; int sum = 1; int items = Integer.parseInt(args[0]); for(int i = 0; i < items; i++) { if(i == 0) { System.out.print(i + " = " + 1 + ", "); continue; } if(i == 1) { System.out.print(i + " = " + 2 + ", "); } //if(ta >= 1) else { tmp = ta; ta = ta + tb; tb = tmp; num = ta + tb; System.out.print(i + " = " + num + ", "); //sum += num; if(i % 2 == 0) { sum+= num; } //continue; } print("sum is " + sum); } } }
But this works as expected.
Java Code:java Fibb 5 == 1, 1, 2, 3, 5
- 01-05-2011, 05:51 AM #6
- 01-05-2011, 05:59 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Im sorry, I forgot I edited it a little. I was trying to do something for project euler and they wanted the fibb series to start with 1, 2, not 1, 1.
so it looks like this
Java Code:java Fibb 5 == 1, 2, 3, 5, 8
- 01-05-2011, 06:16 AM #8
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
should beJava Code:print("sum is " + sum);Java Code:System.out.print("sum is " + sum);
- 01-05-2011, 06:21 AM #9
Java Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-05-2011, 07:39 AM #10
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
actually the output isn't correct.
1.) I think, output (at least) it should beJava Code:[B]INPUT[/B] java Fibb 5 [B]OUTPUT[/B] 0 = 1, 1 = 2, sum is 12 = 3, sum is 43 = 5, sum is 44 = 8, sum is 12
2.) there is no 4 + 3 and 4 + 4 in this sequenceJava Code:0 = 1, 1 = 2, Sum Is 1 + 2 = 3, Sum Is 4 + 3 = 5, Sum Is 4 + 4 = 8, Sum Is 1 + 2
- 01-05-2011, 07:54 AM #11
- 01-05-2011, 08:14 PM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Ya, the sum is to add all the even terms up to the last term less than 4 million. Can't quite get the correct answer on the site somehow. Im noticing with the even terms summed it tends to have strange results..
Is what it output but its apparently not the correct answer, can't figure out why though.Java Code:0 = 1, 1 = 2, sum is 1 2 = 3, sum is 4 3 = 5, sum is 4 4 = 8, sum is 12 5 = 13, sum is 12 6 = 21, sum is 33 7 = 34, sum is 33 8 = 55, sum is 88 9 = 89, sum is 88 10 = 144, sum is 232 11 = 233, sum is 232 12 = 377, sum is 609 13 = 610, sum is 609 14 = 987, sum is 1596 15 = 1597, sum is 1596 16 = 2584, sum is 4180 17 = 4181, sum is 4180 18 = 6765, sum is 10945 19 = 10946, sum is 10945 20 = 17711, sum is 28656 21 = 28657, sum is 28656 22 = 46368, sum is 75024 23 = 75025, sum is 75024 24 = 121393, sum is 196417 25 = 196418, sum is 196417 26 = 317811, sum is 514228 27 = 514229, sum is 514228 28 = 832040, sum is 1346268 29 = 1346269, sum is 1346268 30 = 2178309, sum is 3524577 31 = 3524578, sum is 3524577
- 01-05-2011, 08:23 PM #13
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
Is this still part of your code?Java Code:else { tmp = ta; ta = ta + tb; tb = tmp; num = ta + tb; System.out.print(i + " = " + num + ", "); //sum += num; if(i % 2 == 0)[COLOR="Red"]<--this is not the way to add all even fib numbers[/COLOR] { sum+= num; } //continue; }
- 01-05-2011, 08:29 PM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Yes that line is still there, Im not quite sure why thats wrong though. if I is divisible by 2 it is even, so it should add that term to sum.
Seems to work well in the output code, everytime it reaches an even i term it adds that item.
- 01-05-2011, 08:35 PM #15
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
Do you think they want the sum of the red terms, or of the blue terms? If they ask for the even fib numbers, which one is more logical?Java Code:first second third fourth fifth sixth seventh eighth nineth 1 [COLOR="Red"]1[/COLOR] 2 [COLOR="red"]3[/COLOR] 5 [COLOR="red"]8[/COLOR] 13 [COLOR="red"]21[/COLOR] 34 1 1 [COLOR="Blue"]2[/COLOR] 3 5 [COLOR="blue"]8[/COLOR] 13 21 [COLOR="blue"]34[/COLOR]
- 01-05-2011, 09:18 PM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I got it correct, thanks for pointing out my mistake.
- 01-06-2011, 04:58 AM #17
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
Java Code:public class Fibon { public static void main(String[] args) { if (args.length != 1) { System.out.println("Usage: java Fibon n"); } else { int amount = Integer.parseInt(args[0]); int x=0,y=0,z=1; for (int a=0; a<amount; a++) { x = y; y = z; z = x + y; System.out.print(""+z); if (a != (amount-1)) { System.out.print(", "); } } } } }
C:\> java Fibon 10
1, 2, 3, 5, 8, 13, 21, 34, 55, 89
- 01-06-2011, 06:13 AM #18
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
complete sequence
By aizen92 in forum New To JavaReplies: 25Last Post: 12-29-2010, 11:58 AM -
Fibonacci sequence
By ŖàΫ ỏƒ Ңόρę in forum New To JavaReplies: 6Last Post: 03-25-2010, 06:59 AM -
Linked list sequence and array sequence
By Predz in forum New To JavaReplies: 1Last Post: 12-31-2009, 01:30 AM -
calling sequence
By rocky in forum Web FrameworksReplies: 0Last Post: 04-27-2009, 08:35 PM -
Escape Sequence
By Punter in forum New To JavaReplies: 4Last Post: 02-10-2009, 07:04 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks