diff --git a/src/algorithms/math/index.js b/src/algorithms/math/index.js index 41b2e488..34cecf28 100644 --- a/src/algorithms/math/index.js +++ b/src/algorithms/math/index.js @@ -2,6 +2,8 @@ const extendedEuclidean = require('./extended_euclidean'); const gcd = require('./gcd'); const fastexp = require('./fast_exp'); const lcm = require('./lcm'); +const isprime = require('./is_prime'); +const sumall = require('./sum_all'); const modularInverse = require('./modular_inverse'); module.exports = { @@ -9,5 +11,7 @@ module.exports = { gcd, fastexp, lcm, + isprime, + sumall, modularInverse }; diff --git a/src/algorithms/math/is_prime.js b/src/algorithms/math/is_prime.js new file mode 100644 index 00000000..a51523c3 --- /dev/null +++ b/src/algorithms/math/is_prime.js @@ -0,0 +1,17 @@ +/** + * Checks if a num is prime or not + * @param {Number} num number to check + * @return {Boolean} true if num is prime, false if num is not prime + * + * References: https://javascript.plainenglish.io/11-mathematical-algorithms-in-modern-javascript-bce71318e2da + */ +const isprime = (num) => { + const limit = Math.floor(Math.sqrt(num)); + for (let i = 2; i <= limit; i += 1) { + if (num % i === 0) return false; + } + return num >= 2; +}; + + +module.exports = isprime; diff --git a/src/algorithms/math/sum_all.js b/src/algorithms/math/sum_all.js new file mode 100644 index 00000000..80d836dd --- /dev/null +++ b/src/algorithms/math/sum_all.js @@ -0,0 +1,17 @@ +/** + * Calculates the sum of all numbers in an array + * @param {Array} arr of two ints where arr[0] start, arr[1] end + * @return {Number} sum sum of the range of numberse + */ +const sumall = (arr) => { + let sum = 0; + const min = arr[0]; + const max = arr[1]; + let i = 0; + for (i = min; i <= max; i += 1) { + sum += i; + } + return sum; +}; + +module.exports = sumall; diff --git a/src/algorithms/search/index.js b/src/algorithms/search/index.js index fd58348c..ebbfa87b 100644 --- a/src/algorithms/search/index.js +++ b/src/algorithms/search/index.js @@ -5,6 +5,7 @@ const exponentialsearch = require('./exponential_search'); const interpolationsearch = require('./interpolation_search'); const jumpsearch = require('./jump_search'); const linearsearch = require('./linear_search'); +const naivesearch = require('./naive_search'); const ternarysearch = require('./ternary_search'); module.exports = { @@ -15,5 +16,6 @@ module.exports = { interpolationsearch, jumpsearch, linearsearch, + naivesearch, ternarysearch }; diff --git a/src/algorithms/search/naive_search.js b/src/algorithms/search/naive_search.js new file mode 100644 index 00000000..b50cc416 --- /dev/null +++ b/src/algorithms/search/naive_search.js @@ -0,0 +1,20 @@ +/** + * Binary Search Algorithm + * @param {String} st Array to be searched + * @param {String} pattern Element to be searched + * @return {Number} count Frequency the pattern is in st + */ + +const naivesearch = (st, pattern) => { + let count = 0; + for (let i = 0; i < st.length; i += 1) { + for (let j = 0; j < pattern.length; j += 1) { + if (st[i + j] !== pattern[j]) break; + if (j === pattern.length - 1) count += 1; + } + } + + return count; +}; + +module.exports = naivesearch; diff --git a/test/algorithms/math/testPrime.js b/test/algorithms/math/testPrime.js new file mode 100644 index 00000000..c8f76e67 --- /dev/null +++ b/test/algorithms/math/testPrime.js @@ -0,0 +1,18 @@ +/* eslint-env mocha */ +const isprime = require('../../../src').algorithms.math.isprime; + +const assert = require('assert'); + +describe('isPrime', () => { + it('should return true if number is prime', () => { + assert.equal(isprime(2), true); + assert.equal(isprime(3), true); + assert.equal(isprime(7), true); + assert.equal(isprime(5), true); + }); + it('should return false if number is prime', () => { + assert.equal(isprime(16), false); + assert.equal(isprime(36), false); + assert.equal(isprime(100), false); + }); +}); diff --git a/test/algorithms/math/testSumAll.js b/test/algorithms/math/testSumAll.js new file mode 100644 index 00000000..729e1018 --- /dev/null +++ b/test/algorithms/math/testSumAll.js @@ -0,0 +1,12 @@ +/* eslint-env mocha */ +const sumall = require('../../../src').algorithms.math.sumall; + +const assert = require('assert'); + +describe('sumALL', () => { + it('should return the sum of range', () => { + assert.equal(sumall(2, 5), 14); + assert.equal(sumall(1, 3), 6); + assert.equal(sumall(3, 6), 18); + }); +}); diff --git a/test/algorithms/search/testNaiveSearch.js b/test/algorithms/search/testNaiveSearch.js new file mode 100644 index 00000000..52d7b40a --- /dev/null +++ b/test/algorithms/search/testNaiveSearch.js @@ -0,0 +1,23 @@ +/* eslint-env mocha */ +const naivesearch = require('../../../src').algorithms.search.naivesearch; +const assert = require('assert'); + +describe('Naive Search', () => { + it('should return frequency of the pattern', () => { + const freq = naivesearch('akgjfjhuyutomatokajkhgsvkjrtomato', 'tomato'); + + assert.equal(freq, 2); + }); + + it('should return frequency of the pattern', () => { + const freq = naivesearch('treeseebeetea', 'ee'); + + assert.equal(freq, 3); + }); + + it('should return frequency of the pattern', () => { + const freq = naivesearch('applebottomjeans', 'boots'); + + assert.equal(freq, 0); + }); +});