-
Notifications
You must be signed in to change notification settings - Fork 0
/
ready-for-prime-time.js
57 lines (39 loc) · 1.12 KB
/
ready-for-prime-time.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* http://www.codewars.com/kata/521ef596c106a935c0000519
*
Description:
We need prime numbers and we need them now!
Write a method that takes a maximum bound and returns all primes starting with 0 up-to and including the maximum bound.
For example:
prime(11);
Should return an array that looks like this:
[2,3,5,7,11]
*
*/
let tests = require('./lib/framework.js');
let Test = tests.Test, describe = tests.describe, it = tests.it, before = tests.before, after = tests.after;
function primeTrial(num) {
var sqrt = Math.ceil(Math.sqrt(num)), i = 1;
while (++i <= sqrt) {
if (num % i === 0) {
return false;
}
}
return true;
}
function prime(num) {
if (num < 2) return [];
var retval = [2];
for (var i = 3; i <= num; i += 2) {
if (primeTrial(i)) {
retval.push(i);
}
}
return retval;
}
let result = prime(11);
Test.assertEquals(result[0], 2);
Test.assertEquals(result[1], 3);
Test.assertEquals(result[2], 5);
Test.assertEquals(result[3], 7);
Test.assertEquals(result[4], 11);