forked from manrajgrover/algorithms-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic algorithm for testing in number is prime resolve manrajgr…
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Checks in given number is prime | ||
* @param {Number} n Number | ||
* @return {Boolean} Returns true for prime number, for other false | ||
*/ | ||
function isprime(n) { | ||
if (n < 2) return false; | ||
for (let i = 2; i < n; i += 1) { | ||
if (n % i === 0) { | ||
return false; | ||
} | ||
} | ||
return n > 1; | ||
} | ||
|
||
module.exports = isprime; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* eslint-env mocha */ | ||
const isprime = require('../../../src').algorithms.math.isprime; | ||
|
||
const assert = require('assert'); | ||
|
||
describe('PrimeTest', () => { | ||
it('should return false for negative', () => { | ||
assert.equal(isprime(-1), false); | ||
}); | ||
|
||
it('should return false for 0', () => { | ||
assert.equal(isprime(-1), false); | ||
}); | ||
it('should return false for 2', () => { | ||
assert.equal(isprime(1), false); | ||
}); | ||
|
||
it('should return true for 2', () => { | ||
assert.equal(isprime(2), true); | ||
}); | ||
|
||
it('should return true for 3', () => { | ||
assert.equal(isprime(3), true); | ||
}); | ||
|
||
it('should return true for 31', () => { | ||
assert.equal(isprime(31), true); | ||
}); | ||
|
||
it('should return true for 97', () => { | ||
assert.equal(isprime(97), true); | ||
}); | ||
|
||
it('should return true for 7919', () => { | ||
assert.equal(isprime(7919), true); | ||
}); | ||
}); |