# Thread: Fibbonaci sequence without arrays

1. ## 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
Java Code:
`java Fibb 5`
prints 5 numbers.

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;
}
}
}
}```

2. Senior Member
Join Date
Apr 2010
Location
Philippines
Posts
580
Rep Power
8
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,
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;
}
}
}
}```
Good Luck! :)

3. 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

so
Java Code:
` java Fibb 5`
would produce the first 5 fibbonaci numbers.

4. Senior Member
Join Date
Apr 2010
Location
Philippines
Posts
580
Rep Power
8
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.

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 06:20 AM.

5. completed working 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 = 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);
}
}
}```
The bit with print("sum is " + sum); was something I was trying to do on project euler.

But this works as expected.
Java Code:
```
java Fibb 5  == 1, 1, 2, 3, 5```

6. Originally Posted by sunde887
But this works as expected.
Java Code:
```
java Fibb 5  == 1, 1, 2, 3, 5```
What happened to the first 0 ?

I believe it should be 0, 1, 1, 2, 3, 5. Right?

Goldest

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`

8. Senior Member
Join Date
Apr 2010
Location
Philippines
Posts
580
Rep Power
8
Java Code:
`print("sum is " + sum);`
should be
Java Code:
`System.out.print("sum is " + sum);`

9. Originally Posted by mine0926
Java Code:
`print("sum is " + sum);`
should be
Java Code:
`System.out.print("sum is " + sum);`
Exactly!

Even there is something weird with that import. I don't know why that's used here.

Java Code:
`import static net.mindview.util.Print.*;`
Anyways, seems like problem resolved.

Goldest

10. Senior Member
Join Date
Apr 2010
Location
Philippines
Posts
580
Rep Power
8
Originally Posted by goldest
Exactly!

Even there is something weird with that import. I don't know why that's used here.

Java Code:
`import static net.mindview.util.Print.*;`
Anyways, seems like problem resolved.

Goldest
actually the output isn't correct.
Java 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```
1.) I think, output (at least) it should be
Java Code:
` 0 = 1, 1 = 2, Sum Is 1 + 2 = 3, Sum Is 4 + 3 = 5, Sum Is 4 + 4 = 8, Sum Is 1 + 2`
2.) there is no 4 + 3 and 4 + 4 in this sequence

11. Originally Posted by sunde887
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.
I thought he was doing some changes simultaneously for some new requirements, which resulted in this output.

Goldest

12. 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..
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```
Is what it output but its apparently not the correct answer, can't figure out why though.

13. Senior Member
Join Date
Aug 2010
Posts
127
Rep Power
0
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;
}```
Is this still part of your code?

14. 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.

15. Senior Member
Join Date
Aug 2010
Posts
127
Rep Power
0
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]```
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?

16. I got it correct, thanks for pointing out my mistake.

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

18. I like your code, thanks for posting it, its much nicer then mine was.

#### Posting Permissions

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