Java  1.0
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
Fibonacci Class Reference

Class for generating the Fibonacci sequence. More...

Public Member Functions

 Fibonacci ()
 
 Fibonacci (int n)
 Constructs a Fibonacci sequence from 0 to n-1. More...
 
void fibo (int n)
 Prints the Fibonacci sequence. More...
 
long fiboDumb (int n)
 This is a naive recursive version. More...
 
long fib (int n)
 Generates a Fibonacci number using a recursive algorithm that makes only n recursive calls. More...
 
long Fibo (int n)
 Generates a Fibonacci number using a recursive algorithm that makes only n recursive calls. More...
 
String toString ()
 Used to print a Fibonacci object. More...
 

Static Public Member Functions

static void main (String[] args)
 Main function for testing. More...
 

Private Member Functions

long fib (int n, long s1, long s2)
 Generates a Fibonacci number using a recursive algorithm that makes only n recursive calls. More...
 

Private Attributes

long ncalls = 0
 hold the number of recursive calls More...
 
long[] fibs = null
 hold the Fibonacci sequence More...
 
int nf = 0
 hold the number of terms to be generated More...
 

Detailed Description

Class for generating the Fibonacci sequence.

The Fibonacci Sequence is the series of numbers: \(0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...\)
where the next number is found by adding up the two numbers before it.

To compile:

Author
Paulo Roma
Since
29/12/2012
See also
Wikipedia
Northwestern University
Complexity

Definition at line 16 of file Fibonacci.java.

Constructor & Destructor Documentation

◆ Fibonacci() [1/2]

Fibonacci.Fibonacci ( )

Definition at line 31 of file Fibonacci.java.

Referenced by main().

◆ Fibonacci() [2/2]

Fibonacci.Fibonacci ( int  n)

Constructs a Fibonacci sequence from 0 to n-1.

Parameters
nindex of the last element of the sequence.

Definition at line 38 of file Fibonacci.java.

References Fibo().

Member Function Documentation

◆ fib() [1/2]

long Fibonacci.fib ( int  n)

Generates a Fibonacci number using a recursive algorithm that makes only n recursive calls.

This method just calls the private version of fib.

Parameters
nindex of a element of the sequence.
Returns
the nth Fibonacci number.
See also
fib(int n, long s1, long s2)

Definition at line 115 of file Fibonacci.java.

Referenced by fib(), and main().

◆ fib() [2/2]

long Fibonacci.fib ( int  n,
long  s1,
long  s2 
)
private

Generates a Fibonacci number using a recursive algorithm that makes only n recursive calls.

Parameters
nindex of a element of the sequence.
s1a Fibonacci number.
s2the Fibonacci number following s1.
Returns
the nth Fibonacci number: s1+s2.

Definition at line 129 of file Fibonacci.java.

References fib(), and ncalls.

◆ fibo()

void Fibonacci.fibo ( int  n)

Prints the Fibonacci sequence.

Parameters
nindex of the last element of the sequence.

Definition at line 79 of file Fibonacci.java.

Referenced by main().

◆ Fibo()

long Fibonacci.Fibo ( int  n)

Generates a Fibonacci number using a recursive algorithm that makes only n recursive calls.

Parameters
nindex of a element of the sequence.
Returns
the nth Fibonacci number.

Definition at line 144 of file Fibonacci.java.

References fibs, ncalls, and nf.

Referenced by Fibonacci().

◆ fiboDumb()

long Fibonacci.fiboDumb ( int  n)

This is a naive recursive version.

The number of calls is O(((1+sqrt(5))/2)^n) = O(1.618^n), that is, for n = 20 there are 21891 calls.

Parameters
nindex of a Fibonacci number.
Returns
the nth Fibonacci number.

Definition at line 100 of file Fibonacci.java.

References ncalls.

Referenced by main().

◆ main()

static void Fibonacci.main ( String[]  args)
static

Main function for testing.

Parameters
argsindex of the last Fibobacci number of the sequence.

Definition at line 47 of file Fibonacci.java.

References fib(), fibo(), fiboDumb(), Fibonacci(), and ncalls.

◆ toString()

String Fibonacci.toString ( )

Used to print a Fibonacci object.

Returns
a string with all Fibonacci numbers calculated so far.

Definition at line 172 of file Fibonacci.java.

References fibs, and ncalls.

Member Data Documentation

◆ fibs

long [] Fibonacci.fibs = null
private

hold the Fibonacci sequence

Definition at line 24 of file Fibonacci.java.

Referenced by Fibo(), and toString().

◆ ncalls

long Fibonacci.ncalls = 0
private

hold the number of recursive calls

Definition at line 20 of file Fibonacci.java.

Referenced by fib(), Fibo(), fiboDumb(), main(), and toString().

◆ nf

int Fibonacci.nf = 0
private

hold the number of terms to be generated

Definition at line 28 of file Fibonacci.java.

Referenced by Fibo().


The documentation for this class was generated from the following file: