Skip to content

Commit

Permalink
Merge pull request Chalarangelo#491 from Chalarangelo/atomiks-patch-1
Browse files Browse the repository at this point in the history
[UPDATE] gcd.md
  • Loading branch information
Chalarangelo authored Jan 4, 2018
2 parents 5327b9b + a55d5b3 commit c2764e7
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions snippets/gcd.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

Calculates the greatest common divisor between two or more numbers/arrays.

The `helperGcd `function uses recursion.
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) => {
let data = [].concat(...arr);
const helperGcd = (x, y) => (!y ? x : gcd(y, x % y));
return data.reduce((a, b) => helperGcd(a, b));
const _gcd = (x, y) => (!y ? x : gcd(y, x % y));
return [].concat(...arr).reduce((a, b) => _gcd(a, b));
};
```

Expand Down

0 comments on commit c2764e7

Please sign in to comment.