-
Notifications
You must be signed in to change notification settings - Fork 0
/
prob10.js
34 lines (33 loc) · 881 Bytes
/
prob10.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
// cache the numbers we already no are not prime
// to reduce to O(log * n)
var notprime = {}
module.exports.isPrime = function(num){
var v = false
if (notprime[num])
return false
for(i = 2; i < num; i++){
// if not divisable by any lower number, return true
if((num / i) % 1 == 0 ){
notprime[num] = true
notprime[i*num] = true
return false
}
// if we have gone so far that we checked anything that i could be multipled by
// in order to == num, then no point in continueing to check, this num must be
// prime
if (i > num / i ){ // 3 > (13/3)
return true
}
}
return true
}
module.exports.getSumPrime = function(large){
var sum = 0
for(var i = 2; i < large; i++){
if(notprime[i])
continue
if(module.exports.isPrime(i))
sum += i
}
return sum
}