Skip to content

Commit 7245f3c

Browse files
aayush0325kgrytegunjjoshistdlib-bot
authored
feat: add math/base/special/nonfibonaccif
PR-URL: #3391 Ref: #649 Co-authored-by: Athan Reines <kgryte@gmail.com> Co-authored-by: Gunj Joshi <gunjjoshi8372@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Gunj Joshi <gunjjoshi8372@gmail.com> Co-authored-by: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com>
1 parent 51565a4 commit 7245f3c

File tree

28 files changed

+2212
-0
lines changed

28 files changed

+2212
-0
lines changed
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
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+
# Non-Fibonacci
22+
23+
> Compute the nth [non-Fibonacci][fibonacci-number] single-precision floating-point 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 nth [non-Fibonacci number][fibonacci-number] is given by
30+
31+
<!-- <equation class="equation" label="eq:nonfibonaccif_number" align="center" raw="f(n) = \left \lfloor{ n + 1 + \log_\varphi \biggl( \sqrt{5}( n + 1 + \log_\varphi(\sqrt{5}(n+1))) - 5 + \tfrac{3}{n+1} \biggr) - 2 } \right \rfloor" alt="Formula to compute the nth non-Fibonacci number."> -->
32+
33+
```math
34+
f(n) = \left \lfloor{ n + 1 + \log_\varphi \biggl( \sqrt{5}( n + 1 + \log_\varphi(\sqrt{5}(n+1))) - 5 + \tfrac{3}{n+1} \biggr) - 2 } \right \rfloor
35+
```
36+
37+
<!-- <div class="equation" align="center" data-raw-text="f(n) = \left \lfloor{ n + 1 + \log_\varphi \biggl( \sqrt{5}( n + 1 + \log_\varphi(\sqrt{5}(n+1))) - 5 + \tfrac{3}{n+1} \biggr) - 2 } \right \rfloor" data-equation="eq:nonfibonaccif_number">
38+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/nonfibonaccif/docs/img/equation_nonfibonacci_number.svg" alt="Formula to compute the nth non-Fibonacci number.">
39+
<br>
40+
</div> -->
41+
42+
<!-- </equation> -->
43+
44+
where `φ` is the [golden ratio][golden-ratio].
45+
46+
</section>
47+
48+
<!-- /.intro -->
49+
50+
<!-- Package usage documentation. -->
51+
52+
<section class="usage">
53+
54+
## Usage
55+
56+
```javascript
57+
var nonfibonaccif = require( '@stdlib/math/base/special/nonfibonaccif' );
58+
```
59+
60+
#### nonfibonaccif( n )
61+
62+
Computes the nth [non-Fibonacci][fibonacci-number] single-precision floating-point number.
63+
64+
```javascript
65+
var v = nonfibonaccif( 1 );
66+
// returns 4
67+
68+
v = nonfibonaccif( 2 );
69+
// returns 6
70+
71+
v = nonfibonaccif( 3 );
72+
// returns 7
73+
```
74+
75+
If provided either a non-integer or `n < 1`, the function returns `NaN`.
76+
77+
```javascript
78+
var v = nonfibonaccif( -1 );
79+
// returns NaN
80+
81+
v = nonfibonaccif( 3.14 );
82+
// returns NaN
83+
```
84+
85+
If provided `NaN`, the function returns `NaN`.
86+
87+
```javascript
88+
var v = nonfibonaccif( NaN );
89+
// returns NaN
90+
```
91+
92+
</section>
93+
94+
<!-- /.usage -->
95+
96+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
97+
98+
<section class="notes">
99+
100+
</section>
101+
102+
<!-- /.notes -->
103+
104+
<section class="examples">
105+
106+
## Examples
107+
108+
<!-- eslint no-undef: "error" -->
109+
110+
```javascript
111+
var nonfibonaccif = require( '@stdlib/math/base/special/nonfibonaccif' );
112+
113+
var i;
114+
for ( i = 1; i < 100; i++ ) {
115+
console.log( 'nonfibonaccif(%d) = %d', i, nonfibonaccif( i ) );
116+
}
117+
```
118+
119+
</section>
120+
121+
<!-- /.examples -->
122+
123+
<!-- C interface documentation. -->
124+
125+
* * *
126+
127+
<section class="c">
128+
129+
## C APIs
130+
131+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
132+
133+
<section class="intro">
134+
135+
</section>
136+
137+
<!-- /.intro -->
138+
139+
<!-- C usage documentation. -->
140+
141+
<section class="usage">
142+
143+
### Usage
144+
145+
```c
146+
#include "stdlib/math/base/special/nonfibonaccif.h"
147+
```
148+
149+
#### stdlib_base_nonfibonaccif( x )
150+
151+
Computes the nth non-Fibonacci single-precision floating-point number.
152+
153+
```c
154+
float out = stdlib_base_nonfibonaccif( 1 );
155+
// returns 4.0f
156+
157+
out = stdlib_base_nonfibonaccif( 2 );
158+
// returns 6.0f
159+
```
160+
161+
The function accepts the following arguments:
162+
163+
- **x**: `[in] int32_t` input value.
164+
165+
```c
166+
float stdlib_base_nonfibonaccif( const int32_t x );
167+
```
168+
169+
</section>
170+
171+
<!-- /.usage -->
172+
173+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
174+
175+
<section class="notes">
176+
177+
</section>
178+
179+
<!-- /.notes -->
180+
181+
<!-- C API usage examples. -->
182+
183+
<section class="examples">
184+
185+
### Examples
186+
187+
```c
188+
#include "stdlib/math/base/special/nonfibonaccif.h"
189+
#include <stdio.h>
190+
191+
int main( void ) {
192+
int i;
193+
194+
for ( i = 1; i < 12; i++ ) {
195+
printf( "x: %i => result: %f", i , stdlib_base_nonfibonaccif( i ) );
196+
}
197+
}
198+
```
199+
200+
</section>
201+
202+
<!-- /.examples -->
203+
204+
</section>
205+
206+
<!-- /.c -->
207+
208+
<!-- 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. -->
209+
210+
* * *
211+
212+
<section class="references">
213+
214+
## References
215+
216+
- Gould, H.W. 1965. "Non-Fibonacci Numbers." _Fibonacci Quarterly_, no. 3: 177–83. [&lt;http://www.fq.math.ca/Scanned/3-3/gould.pdf>][@gould:1965a].
217+
- Farhi, Bakir. 2011. "An explicit formula generating the non-Fibonacci numbers." _arXiv_ abs/1105.1127 \[Math.NT] (May): 1–5. [&lt;https://arxiv.org/abs/1105.1127>][@farhi:2011a].
218+
219+
</section>
220+
221+
<!-- /.references -->
222+
223+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
224+
225+
<section class="related">
226+
227+
</section>
228+
229+
<!-- /.related -->
230+
231+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
232+
233+
<section class="links">
234+
235+
[fibonacci-number]: https://en.wikipedia.org/wiki/Fibonacci_number
236+
237+
[golden-ratio]: https://en.wikipedia.org/wiki/Golden_ratio
238+
239+
[@gould:1965a]: http://www.fq.math.ca/Scanned/3-3/gould.pdf
240+
241+
[@farhi:2011a]: https://arxiv.org/abs/1105.1127
242+
243+
<!-- <related-links> -->
244+
245+
<!-- </related-links> -->
246+
247+
</section>
248+
249+
<!-- /.links -->
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 randu = require( '@stdlib/random/array/discrete-uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pkg = require( './../package.json' ).name;
27+
var nonfibonaccif = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var i;
36+
37+
x = randu( 100, 1, 100 );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
y = nonfibonaccif( x[ i % 100 ] );
42+
if ( isnanf( y ) ) {
43+
b.fail( 'should not return NaN' );
44+
}
45+
}
46+
b.toc();
47+
if ( isnanf( y ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 resolve = require( 'path' ).resolve;
24+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
25+
var randu = require( '@stdlib/random/array/discrete-uniform' );
26+
var tryRequire = require( '@stdlib/utils/try-require' );
27+
var bench = require( '@stdlib/bench' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var nonfibonaccif = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( nonfibonaccif instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
x = randu( 100, 1, 100 );
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
y = nonfibonaccif( x[ i % 100 ] );
51+
if ( isnanf( y ) ) {
52+
b.fail( 'should not return NaN' );
53+
}
54+
}
55+
b.toc();
56+
if ( isnanf( y ) ) {
57+
b.fail( 'should not return NaN' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});

0 commit comments

Comments
 (0)