com.internationalnetwork.math
Class Fib

Object
  extended by com.internationalnetwork.math.Fib

public class Fib
extends Object

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


Field Summary
static String VERSION
          Version number of this Package (read-only).
 
Constructor Summary
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.
 
Method Summary
 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.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERSION

public static final String VERSION
Version number of this Package (read-only).

See Also:
Constant Field Values
Constructor Detail

Fib

public Fib()
Instantiate a new Fibonacci Sequence object that starts at number 1.


Fib

public Fib(java.math.BigInteger n)
Instantiate a new Fibonacci Sequence object that starts at the specified number.

Parameters:
n - The number to start the sequence with

Fib

public Fib(long n)
Instantiate a new Fibonacci Sequence object that starts at the specified number.

Parameters:
n - The number to start the sequence with, expressed as an int or a long value

Fib

public Fib(String n)
Instantiate a new Fibonacci Sequence object that starts at the specified number.

Parameters:
n - The number to start the sequence with, expressed as a String
Method Detail

current

public java.math.BigInteger current()
Return the current number in the sequence.


next

public java.math.BigInteger next()
Return the next number in the sequence.


nextInt

public int nextInt()
Return the next number in the sequence, truncated to a 32-bit integer.

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:

  1. 1
  2. 1
  3. 2
  4. 3
  5. 5
  6. 8
  7. 13
  8. 21
  9. 34
  10. 55
  11. 89
  12. 144
  13. 233
  14. 377
  15. 610
  16. 987
  17. 1,597
  18. 2,584
  19. 4,181
  20. 6,765
  21. 10,946
  22. 17,711
  23. 28,657
  24. 46,368
  25. 75,025
  26. 121,393
  27. 196,418
  28. 317,811
  29. 514,229
  30. 832,040
  31. 1,346,269
  32. 2,178,309
  33. 3,524,578
  34. 5,702,887
  35. 9,227,465
  36. 14,930,352
  37. 24,157,817
  38. 39,088,169
  39. 63,245,986
  40. 102,334,155
  41. 165,580,141
  42. 267,914,296
  43. 433,494,437
  44. 701,408,733
  45. 1,134,903,170
  46. 1,836,311,903


nextLong

public long nextLong()
Return the next number in the sequence, truncated to a 64-bit long.

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:

  1. 1
  2. 1
  3. 2
  4. 3
  5. 5
  6. 8
  7. 13
  8. 21
  9. 34
  10. 55
  11. 89
  12. 144
  13. 233
  14. 377
  15. 610
  16. 987
  17. 1,597
  18. 2,584
  19. 4,181
  20. 6,765
  21. 10,946
  22. 17,711
  23. 28,657
  24. 46,368
  25. 75,025
  26. 121,393
  27. 196,418
  28. 317,811
  29. 514,229
  30. 832,040
  31. 1,346,269
  32. 2,178,309
  33. 3,524,578
  34. 5,702,887
  35. 9,227,465
  36. 14,930,352
  37. 24,157,817
  38. 39,088,169
  39. 63,245,986
  40. 102,334,155
  41. 165,580,141
  42. 267,914,296
  43. 433,494,437
  44. 701,408,733
  45. 1,134,903,170
  46. 1,836,311,903
  47. 2,971,215,073
  48. 4,807,526,976
  49. 7,778,742,049
  50. 12,586,269,025
  51. 20,365,011,074
  52. 32,951,280,099
  53. 53,316,291,173
  54. 86,267,571,272
  55. 139,583,862,445
  56. 225,851,433,717
  57. 365,435,296,162
  58. 591,286,729,879
  59. 956,722,026,041
  60. 1,548,008,755,920
  61. 2,504,730,781,961
  62. 4,052,739,537,881
  63. 6,557,470,319,842
  64. 10,610,209,857,723
  65. 17,167,680,177,565
  66. 27,777,890,035,288
  67. 44,945,570,212,853
  68. 72,723,460,248,141
  69. 117,669,030,460,994
  70. 190,392,490,709,135
  71. 308,061,521,170,129
  72. 498,454,011,879,264
  73. 806,515,533,049,393
  74. 1,304,969,544,928,657
  75. 2,111,485,077,978,050
  76. 3,416,454,622,906,707
  77. 5,527,939,700,884,757
  78. 8,944,394,323,791,464
  79. 14,472,334,024,676,221
  80. 23,416,728,348,467,685
  81. 37,889,062,373,143,906
  82. 61,305,790,721,611,591
  83. 99,194,853,094,755,497
  84. 160,500,643,816,367,088
  85. 259,695,496,911,122,585
  86. 420,196,140,727,489,673
  87. 679,891,637,638,612,258
  88. 1,100,087,778,366,101,931
  89. 1,779,979,416,004,714,189
  90. 2,880,067,194,370,816,120
  91. 4,660,046,610,375,530,309
  92. 7,540,113,804,746,346,429