kubeless/functions/isprime.js

27 lines
513 B
JavaScript
Raw Normal View History

2019-04-26 15:34:13 +02:00
module.exports = {
handler: (event, context) => {
num=event.data;
if (num == 1) return "Not Prime";
num += 2;
var upper = Math.sqrt(num);
var sieve = new Array(num)
.join(',').split(',') // get values for map to work
2019-04-26 15:38:09 +02:00
.map(function(){ return "Prime" });
2019-04-26 15:34:13 +02:00
for (var i = 2; i <= num; i++) {
if (sieve[i]) {
for (var j = i * i; j < num; j += i) {
sieve[j] = false;
};
};
};
if (sieve[num-2]) {
return "Prime";
};
else {
return "Not Prime";
};
},
};