README.md (3352B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # gcd 22 23 > Compute the [greatest common divisor][gcd] (gcd). 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 [greatest common divisor][gcd] (gcd) of two non-zero integers `a` and `b` is the largest positive integer which divides both `a` and `b` without a remainder. The gcd is also known as the **greatest common factor** (gcf), **highest common factor** (hcf), **highest common divisor**, and **greatest common measure** (gcm). 30 31 </section> 32 33 <!-- /.intro --> 34 35 <!-- Package usage documentation. --> 36 37 <section class="usage"> 38 39 ## Usage 40 41 ```javascript 42 var gcd = require( '@stdlib/math/base/special/gcd' ); 43 ``` 44 45 #### gcd( a, b ) 46 47 Computes the [greatest common divisor][gcd] (gcd). 48 49 ```javascript 50 var v = gcd( 48, 18 ); 51 // returns 6 52 ``` 53 54 If both `a` and `b` are `0`, the function returns `0`. 55 56 ```javascript 57 var v = gcd( 0, 0 ); 58 // returns 0 59 ``` 60 61 Both `a` and `b` must have integer values; otherwise, the function returns `NaN`. 62 63 ```javascript 64 var v = gcd( 3.14, 18 ); 65 // returns NaN 66 67 v = gcd( 48, 3.14 ); 68 // returns NaN 69 70 v = gcd( NaN, 18 ); 71 // returns NaN 72 73 v = gcd( 48, NaN ); 74 // returns NaN 75 ``` 76 77 </section> 78 79 <!-- /.usage --> 80 81 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 82 83 <section class="notes"> 84 85 </section> 86 87 <!-- /.notes --> 88 89 <!-- Package usage examples. --> 90 91 <section class="examples"> 92 93 ## Examples 94 95 <!-- eslint no-undef: "error" --> 96 97 ```javascript 98 var randu = require( '@stdlib/random/base/randu' ); 99 var round = require( '@stdlib/math/base/special/round' ); 100 var gcd = require( '@stdlib/math/base/special/gcd' ); 101 102 var a; 103 var b; 104 var v; 105 var i; 106 107 for ( i = 0; i < 100; i++ ) { 108 a = round( randu()*50.0 ); 109 b = round( randu()*50.0 ); 110 v = gcd( a, b ); 111 console.log( 'gcd(%d,%d) = %d', a, b, v ); 112 } 113 ``` 114 115 </section> 116 117 <!-- /.examples --> 118 119 <!-- 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. --> 120 121 <section class="references"> 122 123 ## References 124 125 - Stein, Josef. 1967. "Computational problems associated with Racah algebra." _Journal of Computational Physics_ 1 (3): 397–405. doi:[10.1016/0021-9991(67)90047-2][@stein:1967]. 126 127 </section> 128 129 <!-- /.references --> 130 131 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 132 133 <section class="links"> 134 135 [gcd]: http://en.wikipedia.org/wiki/Greatest_common_divisor 136 137 [@stein:1967]: https://doi.org/10.1016/0021-9991(67)90047-2 138 139 </section> 140 141 <!-- /.links -->