README.md (3510B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Fibonacci 22 23 > Compute the nth [Fibonacci number][fibonacci-number]. 24 25 <section class="intro"> 26 27 The [Fibonacci numbers][fibonacci-number] are the integer sequence 28 29 <!-- <equation class="equation" label="eq:fibonacci_sequence" align="center" raw="0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, \ldots" alt="Fibonacci sequence"> --> 30 31 <div class="equation" align="center" data-raw-text="0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, \ldots" data-equation="eq:fibonacci_sequence"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/fibonacci/docs/img/equation_fibonacci_sequence.svg" alt="Fibonacci sequence"> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 The sequence is defined by the recurrence relation 39 40 <!-- <equation class="equation" label="eq:fibonacci_recurrence_relation" align="center" raw="F_n = F_{n-1} + F_{n-2}" alt="Fibonacci sequence recurrence relation"> --> 41 42 <div class="equation" align="center" data-raw-text="F_n = F_{n-1} + F_{n-2}" data-equation="eq:fibonacci_recurrence_relation"> 43 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/fibonacci/docs/img/equation_fibonacci_recurrence_relation.svg" alt="Fibonacci sequence recurrence relation"> 44 <br> 45 </div> 46 47 <!-- </equation> --> 48 49 with seed values `F_0 = 0` and `F_1 = 1`. 50 51 </section> 52 53 <!-- /.intro --> 54 55 <section class="usage"> 56 57 ## Usage 58 59 ```javascript 60 var fibonacci = require( '@stdlib/math/base/special/fibonacci' ); 61 ``` 62 63 #### fibonacci( n ) 64 65 Computes the nth [Fibonacci number][fibonacci-number]. 66 67 ```javascript 68 var v = fibonacci( 0 ); 69 // returns 0 70 71 v = fibonacci( 1 ); 72 // returns 1 73 74 v = fibonacci( 2 ); 75 // returns 1 76 77 v = fibonacci( 3 ); 78 // returns 2 79 80 v = fibonacci( 78 ); 81 // returns 8944394323791464 82 ``` 83 84 If `n > 78`, the function returns `NaN`, as larger [Fibonacci numbers][fibonacci-number] cannot be safely represented in [double-precision floating-point format][ieee754]. 85 86 ```javascript 87 var v = fibonacci( 79 ); 88 // returns NaN 89 ``` 90 91 If not provided a nonnegative integer value, the function returns `NaN`. 92 93 ```javascript 94 var v = fibonacci( 3.14 ); 95 // returns NaN 96 97 v = fibonacci( -1 ); 98 // returns NaN 99 ``` 100 101 If provided `NaN`, the function returns `NaN`. 102 103 ```javascript 104 var v = fibonacci( NaN ); 105 // returns NaN 106 ``` 107 108 </section> 109 110 <!-- /.usage --> 111 112 <section class="notes"> 113 114 </section> 115 116 <!-- /.notes --> 117 118 <section class="examples"> 119 120 ## Examples 121 122 <!-- eslint no-undef: "error" --> 123 124 ```javascript 125 var fibonacci = require( '@stdlib/math/base/special/fibonacci' ); 126 127 var v; 128 var i; 129 130 for ( i = 0; i < 79; i++ ) { 131 v = fibonacci( i ); 132 console.log( v ); 133 } 134 ``` 135 136 </section> 137 138 <!-- /.examples --> 139 140 <section class="links"> 141 142 [fibonacci-number]: https://en.wikipedia.org/wiki/Fibonacci_number 143 144 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 145 146 </section> 147 148 <!-- /.links -->