• 0

[JAVA] Problems with Fibonacci


Question

Hey guys,

I'm supposed to write a program to figure out the nth number in the fibonacci sequence. My book gave us the main method to run:

import java.util.Scanner;
public class FibonacciRunner
{
  public static void main (String[] args)
  {
	Scanner in = new Scanner (System.in);

	System.out.println ("Enter n: ");
	int n = in.nextInt();

	FibonacciGenerator fg = new FibonacciGenerator();

	for (int i = 1; i <= n; i++);
	  System.out.println(fg.nextNumber());
  }
}

and we're supposed to write a class with a constructor anda method to make it work. So far this is what I've got:

import java.util.Scanner;
public class FibonacciGenerator
{ 
  private int a;
  private int b;


  public FibonacciGenerator()
  {
	a=0;
	b=1;
  }
  public int nextNumber()
  {

//I don't know what to put here??

  }
}

the nextNumber() method is supposed to calculate the next number in the sequence, but I have no idea how to calculate it...help?

Link to comment
Share on other sites

19 answers to this question

Recommended Posts

  • 0
The iterative implementation is both faster and simpler.
Here, given the usage code:

	FibonacciGenerator fg = new FibonacciGenerator();

	for (int i = 1; i <= n; i++);
	  System.out.println(fg.nextNumber());

The fastest implementation is certainly to store the last and current number and do a simple addition.

the nextNumber() method is supposed to calculate the next number in the sequence, but I have no idea how to calculate it...help?
If this is a math question, just check out the definition of Fibonnacci numbers on Wikipedia or something, it's simple.

In Java, well you're right about having two integers as fields, one should represent the last value and the other the value before it. Based on that, finding the next number is a single basic arithmetic operation... You also need to update the fields so that the next time nextNumber() is called, it will generate a new number, not the same.

Link to comment
Share on other sites

  • 0
yea, I used this equation when i had to implement the Fibonacci series, much more elegant than simple addition ^^

Care to post a coded example of this? ;)

Link to comment
Share on other sites

  • 0
Care to post a coded example of this? ;)

What's so hard about implementing that? However you can use that as you actually need a "Fibonacci Runner" so you might as well use addition to show all the numbers up to n.

Link to comment
Share on other sites

  • 0

Wouldn't it be easier to just write a sub-routine to take 1+1=2 then add the subsequent number?

So..

1+1=2+1=3+1=4..

and so on and so forth.

I'm not exactly sure it'd be a TRUE Fibonacci, but it'd give the spiral.

Link to comment
Share on other sites

  • 0
What's so hard about implementing that? However you can use that as you actually need a "Fibonacci Runner" so you might as well use addition to show all the numbers up to n.

I did. :rolleyes:

Here's a PHP SPL object implementation http://pastie.org/617788.

However, I'd like to see one using the algorithm supplied and not addition as per my example.

Link to comment
Share on other sites

  • 0
function fibonacci(n:int):int
{
	//Golden Ratio
	var g:Number  = ( 1 + Math.sqrt(5) ) / 2;
	var g1:Number = -1/g;

	// F(n) make sure it's an int as some n give you floating points or round it.
	var f:int = ( Math.pow(g,n) - Math.pow(g1,n) ) / Math.sqrt(5);

	return f;
}

for(var i:int = 1; i <= 30; i++)
{
	fibonacci(i);
}

Link to comment
Share on other sites

  • 0
Care to post a coded example of this? ;)

I was going to say that I would but I can't since it's in Java as well, but mail did it already...

snip

which he shouldn't have done since this is for a class...right?

Link to comment
Share on other sites

  • 0
I was going to say that I would but I can't since it's in Java as well, but mail did it already...

which he shouldn't have done since this is for a class...right?

I was thinking that too but if you can't translate that simple formula into whatever language you need then should you really be doing programming?

Link to comment
Share on other sites

  • 0
I was thinking that too but if you can't translate that simple formula into whatever language you need then should you really be doing programming?

The honest answer to that is no. I hate the class, and frankly I hate having to program...I'm an IT major with a concentration in business...basically I want to do something along the lines of IT management...I'll never use this.

Edit:

I hate having to attempt to program...I obviously can't do it.

Edited by Kev_B
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.