# [JavaScript] Sieve of Eratosthenes

JavaScript implementation of Sieve of Eratosthenes. See from GeeksforGeeks [1]. The JavaScript code is ported from the C/C++ code of GeeksforGeeks.

Input Integer Greater Than 2:

Source Code (HTML):

```Input Integer Greater Than 2: <input type="text" name="n"><br><br>
<button type="button" id="sieve">Run Sieve of Eratosthenes</button><br><br>
<textarea id="result" rows="20" cols="50"></textarea>
```

Source Code (JavaScript):

```function SieveOfEratosthenes(n) {
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
var integers = [];
for (var i = 2; i < n+1; i++) {
integers[i] = true;
}

for (var p = 2; p*p <= n; p++) {
// If integers[p] is not changed, then it is a prime
if (integers[p] == true) {
// Update all multiples of p
for (var i = p * 2; i <= n; i += p) {
integers[i] = false;
}
}
}

// return all prime numbers <= n
var primes = [];
for (var p = 2; p <= n; p++) {
if (integers[p] == true) {
primes.push(p);
}
}

return primes;
}

var elmn = document.querySelector("input[name='n']");
var btn = document.querySelector("#sieve");
var resultElm = document.querySelector("#result");
var n = parseInt(elmn.value);
if (!Number.isInteger(n)) {
resultElm.value = "input must be integer!";
return;
}
if ( n<=2 ) {
resultElm.value = "n must be greater than 2!";
return;
}
result.value = SieveOfEratosthenes(n);
});
```

Tested on: Chromium 65.0.3325.181 on Ubuntu 17.10 (64-bit)

References: