-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathisPrime.js
35 lines (29 loc) · 1000 Bytes
/
isPrime.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Returns true if the given positive integer is prime and false otherwise.
*
* A positive integer is prime if it its only factors are 1 and itself.
*
* See https://en.wikipedia.org/wiki/Prime_number
*
* Primes: 2, 3, 5, 7, 11, 13, 17, ...
*
* @param {number} num - The positive integer whose primality we want to check
* @returns {boolean} True if num is prime and false otherwise
*/
function isPrime(num) {
/*
Your code goes here.
Work out one version that works and don't worry about performance.
If you're having trouble working it out in code, step out of JS-land
and use pen/paper, index cards, etc. — anything that helps you think
about it without getting stuck in JavaScript syntax.
*/
}
if (require.main === module) {
console.log('Running sanity checks for isPrime:');
console.log(isPrime(1) === false);
console.log(isPrime(2) === true);
console.log(isPrime(4) === false);
// Your own sanity checks go here
}
module.exports = isPrime;