Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a function to calculate GCD of Multiple numbers #130

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# algorithms-js [![Build Status](https://travis-ci.org/manrajgrover/algorithms-js.svg?branch=master)](https://travis-ci.org/manrajgrover/algorithms-js) [![Build status](https://ci.appveyor.com/api/projects/status/6l0vybrb4y0c7eh8?svg=true)](https://ci.appveyor.com/project/manrajgrover/algorithms-js) [![npm](https://img.shields.io/npm/v/algorithms-js.svg?maxAge=2592000?style=flat-square)](https://www.npmjs.com/package/algorithms-js) [![npm](https://img.shields.io/npm/dt/algorithms-js.svg?maxAge=2592000?style=flat-square)](https://www.npmjs.com/package/algorithms-js) ![awesome](https://img.shields.io/badge/awesome-yes-green.svg)
> Consumable Data Structures and Algorithms library in JavaScript

> Consumable Data Structures and Algorithms library in JavaScript

## Note

Expand All @@ -27,9 +27,11 @@ Or use `jsdeliver`:
```

## Usage

Library contains both algorithms as well as data structures:

### Data Structures

Currently, library supports following data structures:

- [Doubly Linked List](https://github.com/manrajgrover/algorithms-js/blob/master/src/data-structures/doubly_linked_list.js)
Expand All @@ -42,9 +44,11 @@ Currently, library supports following data structures:
- [Trie](https://github.com/manrajgrover/algorithms-js/blob/master/src/data-structures/trie.js)

### Algorithms

Currently library supports following algorithms:

#### Search

Various Searching algorithms:

- [Binary Search](https://github.com/manrajgrover/algorithms-js/blob/master/src/algorithms/search/binary_search.js)
Expand All @@ -57,6 +61,7 @@ Various Searching algorithms:
- [Ternary Search](https://github.com/manrajgrover/algorithms-js/blob/master/src/algorithms/search/ternary_search.js)

#### Sort

Various Sorting algorithms:

- [Bubble Sort](https://github.com/manrajgrover/algorithms-js/blob/master/src/algorithms/sort/bubble_sort.js)
Expand All @@ -68,19 +73,23 @@ Various Sorting algorithms:
- [Selection Sort](https://github.com/manrajgrover/algorithms-js/blob/master/src/algorithms/sort/selection_sort.js)

#### Math

Various Math algorithms:

- [Extended Euclidean](https://github.com/manrajgrover/algorithms-js/blob/master/src/algorithms/math/extended_euclidean.js)
- [Fast Exponentiation](https://github.com/manrajgrover/algorithms-js/blob/master/src/algorithms/math/fast_exp.js)
- [GCD](https://github.com/manrajgrover/algorithms-js/blob/master/src/algorithms/math/gcd.js)
- [GCD of Multiple Numbers](https://github.com/manrajgrover/algorithms-js/blob/master/src/algorithms/math/gcd_multiple_numbers.js)
- [LCM](https://github.com/manrajgrover/algorithms-js/blob/master/src/algorithms/math/lcm.js)

#### String

Various String algorithms:

- [Levenshtein Distance](https://github.com/manrajgrover/algorithms-js/blob/master/src/algorithms/string/levenshtein_distance.js)

#### Geometry

Various Geometry algorithms:

- [Tangent between circles](https://github.com/manrajgrover/algorithms-js/blob/master/src/algorithms/geometry/tangent_between_circles.js)
Expand Down Expand Up @@ -128,6 +137,7 @@ $ npm run build-dev
```

## Get in touch

Say hi on [twitter](https://twitter.com/manrajsgrover)

## License
Expand Down
56 changes: 41 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions src/algorithms/math/gcd_multiple_numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Calculates GCD of multiple numbers
* @param {Number, Number... Number}
* @return {Number} HCF or GCD of provided numbers
*
* References: https://www.geeksforgeeks.org/python-program-for-gcd-of-more-than-two-or-array-numbers/
*/
function gcd_multiple_numbers() {
if (arguments.length < 2) {
console.error(
`Function gcd_multiple_numbers requires atleast 2 arguments, but only ${arguments.length} provided.`
);
return;
}
const GCD = (a, b) => {
a = Math.abs(a);
b = Math.abs(b);

if (a === 0 || b === 0) {
return 0;
}

if (a === b) {
return a;
}

if (a > b) {
return GCD(a - b, b);
}

return GCD(a, b - a);
};

num1 = arguments[0];
num2 = arguments[1];
let gcd = GCD(num1, num2);
for (let i = 2; i < arguments.length; i++) {
gcd = GCD(gcd, arguments[i]);
}

return gcd;
}

module.exports = gcd_multiple_numbers;
14 changes: 8 additions & 6 deletions src/algorithms/math/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const extendedEuclidean = require('./extended_euclidean');
const gcd = require('./gcd');
const fastexp = require('./fast_exp');
const lcm = require('./lcm');
const modularInverse = require('./modular_inverse');
const extendedEuclidean = require("./extended_euclidean");
const gcd = require("./gcd");
const gcd_multiple_numbers = require("./gcd_multiple_numbers");
const fastexp = require("./fast_exp");
const lcm = require("./lcm");
const modularInverse = require("./modular_inverse");

module.exports = {
extendedEuclidean,
gcd,
gcd_multiple_numbers,
fastexp,
lcm,
modularInverse
modularInverse,
};
Loading