Skip to content

Added Functions: isPrime, sumAll, Naive Search #141

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

Open
wants to merge 17 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
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));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before this you have to verify if the number are greater or equal to 1 or if is a integer

if (num <= 1 || !Number.isInteger(num)) {
return false;
}

for (let i = 2; i <= limit; i += 1) {
if (num % i === 0) return false;
}
return num >= 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
*/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here the wanted function and you implement is different

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put testing for negative number and float

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