|
| 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 | +<!-- C interface documentation. --> |
| 97 | + |
| 98 | +* * * |
| 99 | + |
| 100 | +<section class="c"> |
| 101 | + |
| 102 | +## C APIs |
| 103 | + |
| 104 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 105 | + |
| 106 | +<section class="intro"> |
| 107 | + |
| 108 | +</section> |
| 109 | + |
| 110 | +<!-- /.intro --> |
| 111 | + |
| 112 | +<!-- C usage documentation. --> |
| 113 | + |
| 114 | +<section class="usage"> |
| 115 | + |
| 116 | +### Usage |
| 117 | + |
| 118 | +```c |
| 119 | +#include "stdlib/math/base/special/factorial2.h" |
| 120 | +``` |
| 121 | + |
| 122 | +#### stdlib_base_factorial2( n ) |
| 123 | + |
| 124 | +Evaluates the [double factorial][double-factorial] of `n`. |
| 125 | + |
| 126 | +```c |
| 127 | +double out = stdlib_base_factorial2( 3 ); |
| 128 | +// returns 3 |
| 129 | +``` |
| 130 | + |
| 131 | +The function accepts the following arguments: |
| 132 | + |
| 133 | +- **n**: `[in] int32_t` input value. |
| 134 | + |
| 135 | +```c |
| 136 | +double stdlib_base_factorial2( const int32_t n ); |
| 137 | +``` |
| 138 | +
|
| 139 | +</section> |
| 140 | +
|
| 141 | +<!-- /.usage --> |
| 142 | +
|
| 143 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 144 | +
|
| 145 | +<section class="notes"> |
| 146 | +
|
| 147 | +</section> |
| 148 | +
|
| 149 | +<!-- /.notes --> |
| 150 | +
|
| 151 | +<!-- C API usage examples. --> |
| 152 | +
|
| 153 | +<section class="examples"> |
| 154 | +
|
| 155 | +### Examples |
| 156 | +
|
| 157 | +```c |
| 158 | +#include "stdlib/math/base/special/factorial2.h" |
| 159 | +#include <stdio.h> |
| 160 | +#include <stdint.h> |
| 161 | +
|
| 162 | +int main( void ) { |
| 163 | + const int32_t x[] = { 1, 10, 1, 301, 302 }; |
| 164 | +
|
| 165 | + double b; |
| 166 | + int i; |
| 167 | + for ( i = 0; i < 5; i++ ){ |
| 168 | + b = stdlib_base_factorial2( x[ i ] ); |
| 169 | + printf ( "factorial2(%d) = %lf\n", x[ i ], b ); |
| 170 | + } |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +</section> |
| 175 | + |
| 176 | +<!-- /.examples --> |
| 177 | + |
| 178 | +</section> |
| 179 | + |
| 180 | +<!-- /.c --> |
| 181 | + |
| 182 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 183 | + |
| 184 | +<section class="related"> |
| 185 | + |
| 186 | +* * * |
| 187 | + |
| 188 | +## See Also |
| 189 | + |
| 190 | +- <span class="package-name">[`@stdlib/math/base/special/factorial`][@stdlib/math/base/special/factorial]</span><span class="delimiter">: </span><span class="description">evaluate a factorial.</span> |
| 191 | + |
| 192 | +</section> |
| 193 | + |
| 194 | +<!-- /.related --> |
| 195 | + |
| 196 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 197 | + |
| 198 | +<section class="links"> |
| 199 | + |
| 200 | +[double-factorial]: https://en.wikipedia.org/wiki/Double_factorial |
| 201 | + |
| 202 | +<!-- <related-links> --> |
| 203 | + |
| 204 | +[@stdlib/math/base/special/factorial]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/factorial |
| 205 | + |
| 206 | +<!-- </related-links> --> |
| 207 | + |
| 208 | +</section> |
| 209 | + |
| 210 | +<!-- /.links --> |
0 commit comments