Prepare repository for merge
This commit is contained in:
25
javascript/snippets/gcd.md
Normal file
25
javascript/snippets/gcd.md
Normal file
@ -0,0 +1,25 @@
|
||||
---
|
||||
title: Greatest common divisor
|
||||
type: snippet
|
||||
tags: [math,algorithm,recursion]
|
||||
cover: flower-pond
|
||||
dateModified: 2020-12-29T12:36:50+02:00
|
||||
---
|
||||
|
||||
Calculates the greatest common divisor between two or more numbers/arrays.
|
||||
|
||||
- The inner `_gcd` function uses recursion.
|
||||
- Base case is when `y` equals `0`. In this case, return `x`.
|
||||
- Otherwise, return the GCD of `y` and the remainder of the division `x / y`.
|
||||
|
||||
```js
|
||||
const gcd = (...arr) => {
|
||||
const _gcd = (x, y) => (!y ? x : gcd(y, x % y));
|
||||
return [...arr].reduce((a, b) => _gcd(a, b));
|
||||
};
|
||||
```
|
||||
|
||||
```js
|
||||
gcd(8, 36); // 4
|
||||
gcd(...[12, 8, 32]); // 4
|
||||
```
|
||||
Reference in New Issue
Block a user