Skip to content

Commit c0dab03

Browse files
rgizzkgryte
andauthored
feat: add math/base/special/factorial2
Closes: #44 PR-URL: #1112 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent ae9cdb7 commit c0dab03

File tree

14 files changed

+969
-0
lines changed

14 files changed

+969
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 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+
# factorial2
22+
23+
> [Double factorial][double-factorial] function.
24+
25+
<section class="intro">
26+
27+
The [double factorial][double-factorial] of a number `n`, denoted `n!!`, is defined as the product of all the positive integers up to `n` that have the same parity (odd or even) as `n`.
28+
29+
Thus, for example, `5!!` is `5 * 3 * 1 = 15` and `8!!` is `8 * 6 * 4 * 2 = 384`.
30+
31+
</section>
32+
33+
<!-- /.intro -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var factorial2 = require( '@stdlib/math/base/special/factorial2' );
41+
```
42+
43+
#### factorial2( n )
44+
45+
Evaluates the [double factorial][double-factorial] of `n`.
46+
47+
```javascript
48+
var v = factorial2( 2 );
49+
// returns 2
50+
51+
v = factorial2( 3 );
52+
// returns 3
53+
54+
v = factorial2( 0 );
55+
// returns 1
56+
57+
v = factorial2( 4 );
58+
// returns 8
59+
60+
v = factorial2( 5 );
61+
// returns 15
62+
63+
v = factorial2( NaN );
64+
// returns NaN
65+
66+
v = factorial2( 301 );
67+
// returns Infinity
68+
```
69+
70+
</section>
71+
72+
<!-- /.usage -->
73+
74+
<section class="examples">
75+
76+
## Examples
77+
78+
<!-- eslint no-undef: "error" -->
79+
80+
```javascript
81+
var oneTo = require( '@stdlib/array/base/one-to' );
82+
var factorial2 = require( '@stdlib/math/base/special/factorial2' );
83+
84+
var values = oneTo( 300 );
85+
86+
var i;
87+
for ( i = 0; i < values.length; i++ ) {
88+
console.log( 'f(%d): %d', values[ i ], factorial2( values[ i ] ) );
89+
}
90+
```
91+
92+
</section>
93+
94+
<!-- /.examples -->
95+
96+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
97+
98+
<section class="related">
99+
100+
</section>
101+
102+
<!-- /.related -->
103+
104+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
105+
106+
<section class="links">
107+
108+
[double-factorial]: https://en.wikipedia.org/wiki/Double_factorial
109+
110+
</section>
111+
112+
<!-- /.links -->
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 pkg = require( './../package.json' ).name;
26+
var factorial2 = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var y;
33+
var i;
34+
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
y = factorial2( i%301 );
38+
if ( isnan( y ) ) {
39+
b.fail( 'should not return NaN' );
40+
}
41+
}
42+
b.toc();
43+
if ( isnan( y ) ) {
44+
b.fail( 'should not return NaN' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#/
2+
# @license Apache-2.0
3+
#
4+
# Copyright (c) 2023 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+
20+
# VARIABLES #
21+
22+
ifndef VERBOSE
23+
QUIET := @
24+
endif
25+
26+
# Specify the path to Boost:
27+
BOOST ?=
28+
29+
# Determine the OS:
30+
#
31+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
32+
# [2]: http://stackoverflow.com/a/27776822/2225624
33+
OS ?= $(shell uname)
34+
ifneq (, $(findstring MINGW,$(OS)))
35+
OS := WINNT
36+
else
37+
ifneq (, $(findstring MSYS,$(OS)))
38+
OS := WINNT
39+
else
40+
ifneq (, $(findstring CYGWIN,$(OS)))
41+
OS := WINNT
42+
endif
43+
endif
44+
endif
45+
46+
# Define the program used for compiling C++ source files:
47+
ifdef CXX_COMPILER
48+
CXX := $(CXX_COMPILER)
49+
else
50+
CXX := g++
51+
endif
52+
53+
# Define the command-line options when compiling C++ files:
54+
CXXFLAGS ?= \
55+
-std=c++11 \
56+
-O3 \
57+
-Wall \
58+
-pedantic
59+
60+
# Determine whether to generate [position independent code][1]:
61+
#
62+
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
63+
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
64+
ifeq ($(OS), WINNT)
65+
fPIC ?=
66+
else
67+
fPIC ?= -fPIC
68+
endif
69+
70+
# List of C++ targets:
71+
cxx_targets := benchmark.out
72+
73+
74+
# TARGETS #
75+
76+
# Default target.
77+
#
78+
# This target is the default target.
79+
80+
all: $(cxx_targets)
81+
82+
.PHONY: all
83+
84+
85+
# Compile C++ source.
86+
#
87+
# This target compiles C++ source files.
88+
89+
$(cxx_targets): %.out: %.cpp $(BOOST)
90+
$(QUIET) $(CXX) $(CXXFLAGS) $(fPIC) -I $(BOOST) -o $@ $< -lm
91+
92+
93+
# Run a benchmark.
94+
#
95+
# This target runs a benchmark.
96+
97+
run: $(cxx_targets)
98+
$(QUIET) ./$<
99+
100+
.PHONY: run
101+
102+
103+
# Perform clean-up.
104+
#
105+
# This target removes generated files.
106+
107+
clean:
108+
$(QUIET) -rm -f *.o *.out
109+
110+
.PHONY: clean

0 commit comments

Comments
 (0)