Skip to content

Commit fa2359f

Browse files
authored
feat: add math/iter/sequences/tribonacci
PR-URL: #1393 Closes: #1331 Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent ee1c957 commit fa2359f

File tree

14 files changed

+1368
-0
lines changed

14 files changed

+1368
-0
lines changed
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# iterTribonacciSeq
22+
23+
> Create an iterator which generates a [tribonacci sequence][tribonacci-number].
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
The [Tribonacci numbers][tribonacci-number] are the integer sequence
30+
31+
<!-- <equation class="equation" label="eq:tribonacci_sequence" align="center" raw="0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, \ldots" alt="Tribonacci sequence"> -->
32+
33+
```math
34+
0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, \ldots
35+
```
36+
37+
<!-- <div class="equation" align="center" data-raw-text="0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, \ldots" data-equation="eq:tribonacci_sequence">
38+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@3249a68fb57cd71148f87ef3b2774be70a04d80a/lib/node_modules/@stdlib/math/base/special/tribonacci/docs/img/equation_tribonacci_sequence.svg" alt="Tribonacci sequence">
39+
<br>
40+
</div> -->
41+
42+
<!-- </equation> -->
43+
44+
The sequence is defined by the recurrence relation
45+
46+
<!-- <equation class="equation" label="eq:tribonacci_recurrence_relation" align="center" raw="F_n = F_{n-1} + F_{n-2} + F_{n-3}" alt="Tribonacci sequence recurrence relation"> -->
47+
48+
```math
49+
F_n = F_{n-1} + F_{n-2} + F_{n-3}
50+
```
51+
52+
<!-- <div class="equation" align="center" data-raw-text="F_n = F_{n-1} + F_{n-2} + F_{n-3}" data-equation="eq:tribonacci_recurrence_relation">
53+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@3249a68fb57cd71148f87ef3b2774be70a04d80a/lib/node_modules/@stdlib/math/base/special/tribonacci/docs/img/equation_tribonacci_recurrence_relation.svg" alt="Tribonacci sequence recurrence relation">
54+
<br>
55+
</div> -->
56+
57+
<!-- </equation> -->
58+
59+
with seed values `F_0 = 0`, `F_1 = 0`, and `F_2 = 1`.
60+
61+
</section>
62+
63+
<!-- /.intro -->
64+
65+
<!-- Package usage documentation. -->
66+
67+
<section class="usage">
68+
69+
## Usage
70+
71+
```javascript
72+
var iterTribonacciSeq = require( '@stdlib/math/iter/sequences/tribonacci' );
73+
```
74+
75+
#### iterTribonacciSeq( \[options] )
76+
77+
Returns an iterator which generates a [Tribonacci sequence][tribonacci-number].
78+
79+
```javascript
80+
var it = iterTribonacciSeq();
81+
// returns <Object>
82+
83+
var v = it.next().value;
84+
// returns 0
85+
86+
v = it.next().value;
87+
// returns 0
88+
89+
v = it.next().value;
90+
// returns 1
91+
92+
// ...
93+
```
94+
95+
The returned iterator protocol-compliant object has the following properties:
96+
97+
- **next**: function which returns an iterator protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the iterator is finished.
98+
- **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.
99+
100+
The function supports the following `options`:
101+
102+
- **iter**: number of iterations. Default: `64`.
103+
104+
The returned iterator can only generate the first `64` [Tribonacci numbers][tribonacci-number], as larger [Tribonacci numbers][tribonacci-number] cannot be safely represented in [double-precision floating-point format][ieee754]. By default, the function returns an iterator which generates all `64` numbers. To limit the number of iterations, set the `iter` option.
105+
106+
```javascript
107+
var opts = {
108+
'iter': 3
109+
};
110+
var it = iterTribonacciSeq( opts );
111+
// returns <Object>
112+
113+
var v = it.next().value;
114+
// returns 0
115+
116+
v = it.next().value;
117+
// returns 0
118+
119+
v = it.next().value;
120+
// returns 1
121+
122+
var bool = it.next().done;
123+
// returns true
124+
```
125+
126+
</section>
127+
128+
<!-- /.usage -->
129+
130+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
131+
132+
<section class="notes">
133+
134+
## Notes
135+
136+
- If an environment supports `Symbol.iterator`, the returned iterator is iterable.
137+
138+
</section>
139+
140+
<!-- /.notes -->
141+
142+
<!-- Package usage examples. -->
143+
144+
<section class="examples">
145+
146+
## Examples
147+
148+
<!-- eslint no-undef: "error" -->
149+
150+
```javascript
151+
var iterTribonacciSeq = require( '@stdlib/math/iter/sequences/tribonacci' );
152+
153+
// Create an iterator:
154+
var it = iterTribonacciSeq();
155+
156+
// Perform manual iteration...
157+
var v;
158+
while ( true ) {
159+
v = it.next();
160+
if ( v.done ) {
161+
break;
162+
}
163+
console.log( v.value );
164+
}
165+
```
166+
167+
</section>
168+
169+
<!-- /.examples -->
170+
171+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
172+
173+
<section class="references">
174+
175+
</section>
176+
177+
<!-- /.references -->
178+
179+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
180+
181+
<section class="related">
182+
183+
* * *
184+
185+
## See Also
186+
187+
- <span class="package-name">[`@stdlib/math/base/special/tribonacci`][@stdlib/math/base/special/tribonacci]</span><span class="delimiter">: </span><span class="description">compute the nth Tribonacci number.</span>
188+
- <span class="package-name">[`@stdlib/math/base/special/fibonacci`][@stdlib/math/base/special/fibonacci]</span><span class="delimiter">: </span><span class="description">compute the nth Fibonacci number.</span>
189+
- <span class="package-name">[`@stdlib/math/iter/sequences/lucas`][@stdlib/math/iter/sequences/lucas]</span><span class="delimiter">: </span><span class="description">create an iterator which generates a Lucas sequence.</span>
190+
191+
</section>
192+
193+
<!-- /.related -->
194+
195+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
196+
197+
<section class="links">
198+
199+
[tribonacci-number]: https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Tribonacci_numbers
200+
201+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
202+
203+
<!-- <related-links> -->
204+
205+
[@stdlib/math/base/special/tribonacci]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/tribonacci
206+
207+
[@stdlib/math/iter/sequences/lucas]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/iter/sequences/lucas
208+
209+
[@stdlib/math/base/special/fibonacci]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/fibonacci
210+
211+
<!-- </related-links> -->
212+
213+
</section>
214+
215+
<!-- /.links -->
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
26+
var pkg = require( './../package.json' ).name;
27+
var iterTribonacciSeq = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var iter;
34+
var i;
35+
36+
b.tic();
37+
for ( i = 0; i < b.iterations; i++ ) {
38+
iter = iterTribonacciSeq();
39+
if ( typeof iter !== 'object' ) {
40+
b.fail( 'should return an object' );
41+
}
42+
}
43+
b.toc();
44+
if ( !isIteratorLike( iter ) ) {
45+
b.fail( 'should return an iterator protocol-compliant object' );
46+
}
47+
b.pass( 'benchmark finished' );
48+
b.end();
49+
});
50+
51+
bench( pkg+'::iteration', function benchmark( b ) {
52+
var iter;
53+
var z;
54+
var i;
55+
56+
iter = iterTribonacciSeq();
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
z = iter.next().value;
61+
if ( isnan( z ) ) {
62+
b.fail( 'should not be NaN' );
63+
}
64+
}
65+
b.toc();
66+
if ( isnan( z ) ) {
67+
b.fail( 'should not be NaN' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
});
Loading

0 commit comments

Comments
 (0)