Skip to content

Commit 0116e3e

Browse files
Harsh MathurHarsh Mathur
authored andcommitted
feat: add math/base/special/lcmf
1 parent 66b5efc commit 0116e3e

File tree

24 files changed

+2012
-0
lines changed

24 files changed

+2012
-0
lines changed
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
-->
18+
19+
# lcmf
20+
21+
> Compute the [least common multiple][lcm] of all integers from 1 up to a specified integer \( n \).
22+
23+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
24+
25+
<section class="intro">
26+
27+
The [least common multiple][lcm] of a set of integers is the smallest positive integer that is divisible by each number in the set. This function computes the LCM[lcm] for all integers from 1 up to a given integer \( n \), which is especially useful in various mathematical and computational applications.
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var lcmf = require( '@stdlib/math/base/special/lcmf' );
41+
```
42+
43+
### lcmf(n)
44+
Computes the [least common multiple][lcm] for all integers from 1 up to n.
45+
46+
```javascript
47+
var v = lcmf( 10 );
48+
// returns 2520
49+
```
50+
51+
If n is less than 1, the function returns 0.
52+
53+
```javascript
54+
var v = lcmf( 0 );
55+
// returns 0
56+
57+
v = lcmf( -5 );
58+
// returns 0
59+
```
60+
61+
The input n must be an integer; otherwise, the function returns NaN.
62+
63+
```javascript
64+
var v = lcmf( 3.14 );
65+
// returns NaN
66+
67+
v = lcmf( NaN );
68+
// returns NaN
69+
```
70+
71+
</section>
72+
73+
<!-- /.usage -->
74+
75+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
76+
77+
<section class="notes">
78+
79+
</section>
80+
81+
<!-- /.notes -->
82+
83+
<!-- Package usage examples. -->
84+
85+
<section class="examples">
86+
87+
## Examples
88+
89+
<!-- eslint no-undef: "error" -->
90+
91+
```javascript
92+
var lcmf = require( '@stdlib/math/base/special/lcmf' );
93+
94+
var v;
95+
var i;
96+
97+
for ( i = 1; i <= 10; i++ ) {
98+
v = lcmf( i );
99+
console.log( 'lcm(1 to %d) = %d', i, v );
100+
}
101+
```
102+
103+
</section>
104+
105+
<!-- /.examples -->
106+
107+
<!-- C interface documentation. -->
108+
109+
* * *
110+
111+
<section class="c">
112+
113+
## C APIs
114+
115+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
116+
117+
<section class="intro">
118+
119+
</section>
120+
121+
<!-- /.intro -->
122+
123+
<!-- C usage documentation. -->
124+
125+
<section class="usage">
126+
127+
### Usage
128+
129+
```c
130+
#include "stdlib/math/base/special/lcmf.h"
131+
```
132+
133+
### stdlib_base_lcmf( n )
134+
135+
Computes the [least common multiple][lcm] for all integers from 1 up to n.
136+
137+
```c
138+
double v = stdlib_base_lcmf( 10 );
139+
// returns 2520.0
140+
```
141+
142+
The function accepts the following argument:
143+
144+
- **n**: `[in] double` input value.
145+
146+
```c
147+
double stdlib_base_lcmf( const double n );
148+
```
149+
150+
</section>
151+
152+
<!-- /.usage -->
153+
154+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
155+
156+
<section class="notes">
157+
158+
</section>
159+
160+
<!-- /.notes -->
161+
162+
<!-- C API usage examples. -->
163+
164+
<section class="examples">
165+
166+
### Examples
167+
168+
```c
169+
#include "stdlib/math/base/special/lcmf.h"
170+
#include <stdio.h>
171+
172+
int main( void ) {
173+
double v;
174+
int i;
175+
for ( i = 1; i <= 10; i++ ) {
176+
v = stdlib_base_lcmf( i );
177+
printf( "lcm(1 to %d) = %lf\n", i, v );
178+
}
179+
}
180+
```
181+
182+
</section>
183+
184+
<!-- /.examples -->
185+
186+
</section>
187+
188+
<!-- /.c -->
189+
190+
<!-- 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. -->
191+
192+
<section class="references">
193+
194+
</section>
195+
196+
<!-- /.references -->
197+
198+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
199+
200+
<section class="related">
201+
202+
* * *
203+
204+
## See Also
205+
206+
- <span class="package-name">[`@stdlib/math/base/special/gcd`][@stdlib/math/base/special/gcd]</span><span class="delimiter">: </span><span class="description">compute the greatest common divisor (gcd).</span>
207+
208+
</section>
209+
210+
<!-- /.related -->
211+
212+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
213+
214+
<section class="links">
215+
216+
[lcm]: https://en.wikipedia.org/wiki/Least_common_multiple
217+
218+
<!-- <related-links> -->
219+
220+
[@stdlib/math/base/special/gcd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/gcd
221+
222+
<!-- </related-links> -->
223+
224+
</section>
225+
226+
<!-- /.links -->
227+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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/base/randu' );
25+
var round = require( '@stdlib/math/base/special/round' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pkg = require( './../package.json' ).name;
28+
var lcmf = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var x;
35+
var y;
36+
var z;
37+
var i;
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
x = round( randu() * 50.0 );
42+
z = lcmf( x );
43+
if ( isnan( z ) ) {
44+
b.fail( 'should not return NaN' );
45+
}
46+
}
47+
b.toc();
48+
if ( isnan( z ) ) {
49+
b.fail( 'should not return NaN' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 bench = require( '@stdlib/bench' );
25+
var randu = require( '@stdlib/random/base/randu' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var lcmf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( lcmf 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+
b.tic();
47+
for( i = 0;i < b.iterations; i++ ) {
48+
x = ( randu() * 100.0 ) + 1.0;
49+
y = lcmf( x );
50+
if( isnan( y ) ) {
51+
b.fail( 'should not return NaN' );
52+
}
53+
}
54+
b.toc();
55+
if( isnan( y ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});

0 commit comments

Comments
 (0)