public class Fib
extends Object
Fib
class is an implementation of the famous recursive
Fibonacci Sequence where each number is the sum of the previous two
numbers. To make this sequence function, however, the first number
must be non-zero (the number 1 is normally selected). The Fibonacci
Sequence begins like this when the first number is 1:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, and so on...
History
Although this sequence was first documented by a scholar in India named Hemachandra Suri, at around 1150 AD, it was named after filius Bonaccio (a.k.a., "Leonardo of Pisa") as "Fibonacci" when he published a book in 1202 entitled Liber Abaci (a.k.a., "Liber Abbaci") that introduced this sequence along with many other useful math concepts.
A notable property of the Fibonacci Sequence (a.k.a., "Fibonacci Numbers") is that every 3rd number is even. This is due to the nature of addition where the sum of two odd numbers is always even.
Technical details
A parameterless constructor is provided that starts the sequence with1, and then another constructor is provided that allows you to specify the first number. The next() method traverses this recursive sequence, one step at a time, and a variety of other methods are provided for convenience.
Internally we don't shift numbers around within an array, or swap their pointers between two variables, rather we use a boolean to keep track of which one of the two previous numbers is older. This ensures better overall performance since fewer CPU cycles are used.
Modifier and Type | Field and Description |
---|---|
static String |
VERSION
Version number of this Package (read-only).
|
Constructor and Description |
---|
Fib()
Instantiate a new Fibonacci Sequence object that starts at number 1.
|
Fib(java.math.BigInteger n)
Instantiate a new Fibonacci Sequence object that starts at the specified
number.
|
Fib(long n)
Instantiate a new Fibonacci Sequence object that starts at the specified
number.
|
Fib(String n)
Instantiate a new Fibonacci Sequence object that starts at the specified
number.
|
Modifier and Type | Method and Description |
---|---|
java.math.BigInteger |
current()
Return the current number in the sequence.
|
java.math.BigInteger |
next()
Return the next number in the sequence.
|
int |
nextInt()
Return the next number in the sequence, truncated to a 32-bit integer.
|
long |
nextLong()
Return the next number in the sequence, truncated to a 64-bit long.
|
public static final String VERSION
public Fib()
public Fib(java.math.BigInteger n)
n
- The number to start the sequence withpublic Fib(long n)
n
- The number to start the sequence with, expressed as an int or a long
valuepublic Fib(String n)
n
- The number to start the sequence with, expressed as a Stringpublic java.math.BigInteger current()
public java.math.BigInteger next()
public int nextInt()
WARNING: Do NOT rely on this for sequences with more than 46 numbers. The highest Fibonacci Sequence value that "int" can support is 1,836,311,903; numbers beyond this will not be accurate because larger values will be truncated to 32 bits (although the number maintained by the Fib object won't be tainted).
The full set of 46 numbers that make up the 32-bit Fibonacci Sequence are:
public long nextLong()
WARNING: Do NOT rely on this for sequences with more than 92 numbers. The highest Fibonacci Sequence value that "long" can support is 7,540,113,804,746,346,429; numbers beyond this won't be accurate because larger values will be truncated to 32 bits (although the number maintained by the Fib object won't be tainted).
The full set of 91 numbers that make up the 64-bit Fibonacci Sequence are: