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..da62d747 --- /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++){ + if (n % i === 0) return false; + } + return n >= 2; + }; + + module.exports = isprime; + \ No newline at end of file diff --git a/src/algorithms/math/sum_all.js b/src/algorithms/math/sum_all.js new file mode 100644 index 00000000..43d94ba9 --- /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) => { + var sum =0; + var min = arr[0]; + var max = arr[1]; + for (var i = min; i <= max; i++) { + 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..d8790409 --- /dev/null +++ b/src/algorithms/search/naive_search.js @@ -0,0 +1,21 @@ +/** + * 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++) { + for(let j = 0; j < pattern.length; j++) { + if(st[i + j] !== pattern[j]) break; + if(j === pattern.length - 1) count++; + } + } + + return count; + } + + module.exports = naivesearch; \ No newline at end of file diff --git a/test/algorithms/math/testPrime.js b/test/algorithms/math/testPrime.js new file mode 100644 index 00000000..a2f2717a --- /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(33), true); + assert.equal(isprime(7), true); + assert.equal(isprime(37), true); + }); + it('should return false if number is prime', () => { + assert.equal(isprime(16), false); + assert.equal(isprime(36), false); + assert.equal(isprime(100), false); + }); +}); \ No newline at end of file diff --git a/test/algorithms/math/testSumAll.js b/test/algorithms/math/testSumAll.js new file mode 100644 index 00000000..ee049eca --- /dev/null +++ b/test/algorithms/math/testSumAll.js @@ -0,0 +1,14 @@ +/* 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); + assert.equal(sumall(37), true); + }); + +}); \ No newline at end of file diff --git a/test/algorithms/search/testNaiveSearch.js b/test/algorithms/search/testNaiveSearch.js new file mode 100644 index 00000000..801d487f --- /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, 1); + }); + + 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); + }); +});