How to create Fibonacci Sequence in Java

Posted by rfkrocktk on Stack Overflow See other posts from Stack Overflow or by rfkrocktk
Published on 2009-06-25T17:20:15Z Indexed on 2010/04/11 6:13 UTC
Read the original article Hit count: 480

Filed under:
|

I really suck at math. I mean, I REALLY suck at math. I'm trying to make a simple fibonacci sequence class for an algorithm I'll be using. I have seen the python example which looks something like this:

a = 0
b = 1
while b < 10:
    print b
    a, b = b, b+a

The problem is that I can't really make this work in any other language. I'd like to make it work in Java, since I can pretty much translate it into the other languages I use from there. This is the general thought:

    public class FibonacciAlgorithm {

	private Integer a = 0;

	private Integer b = 1;

	public FibonacciAlgorithm() {

	}

	public Integer increment() {
		a = b;
        b = a + b;
		return value;
	}

	public Integer getValue() {
		return b;
	}
}

All that I end up with is doubling, which I could do with multiplication :( Can anyone help me out? Math pwns me.

© Stack Overflow or respective owner

Related posts about java

Related posts about python