Skip to content

Added Functions: Sum All, Naive Search, Is Prime #1

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

Merged
merged 4 commits into from
Jan 23, 2023
Merged
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
4 changes: 4 additions & 0 deletions src/algorithms/math/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ 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 = {
extendedEuclidean,
gcd,
fastexp,
lcm,
isprime,
sumall,
modularInverse
};
17 changes: 17 additions & 0 deletions src/algorithms/math/is_prime.js
Original file line number Diff line number Diff line change
@@ -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;

17 changes: 17 additions & 0 deletions src/algorithms/math/sum_all.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions src/algorithms/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -15,5 +16,6 @@ module.exports = {
interpolationsearch,
jumpsearch,
linearsearch,
naivesearch,
ternarysearch
};
21 changes: 21 additions & 0 deletions src/algorithms/search/naive_search.js
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 18 additions & 0 deletions test/algorithms/math/testPrime.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
14 changes: 14 additions & 0 deletions test/algorithms/math/testSumAll.js
Original file line number Diff line number Diff line change
@@ -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);
});

});
23 changes: 23 additions & 0 deletions test/algorithms/search/testNaiveSearch.js
Original file line number Diff line number Diff line change
@@ -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);
});
});