mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
Re-orgainze files and folders in the repository (#172)
* Re-orgainze files and folders in the repository * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
31
Dynamic-Programming/SieveOfEratosthenes.js
Normal file
31
Dynamic-Programming/SieveOfEratosthenes.js
Normal file
@ -0,0 +1,31 @@
|
||||
function sieveOfEratosthenes (n) {
|
||||
/*
|
||||
* Calculates prime numbers till a number n
|
||||
* :param n: Number upto which to calculate primes
|
||||
* :return: A boolean list contaning only primes
|
||||
*/
|
||||
const primes = new Array(n + 1)
|
||||
primes.fill(true) // set all as true initially
|
||||
primes[0] = primes[1] = false // Handling case for 0 and 1
|
||||
const sqrtn = Math.ceil(Math.sqrt(n))
|
||||
for (let i = 2; i <= sqrtn; i++) {
|
||||
if (primes[i]) {
|
||||
for (let j = 2 * i; j <= n; j += i) {
|
||||
primes[j] = false
|
||||
}
|
||||
}
|
||||
}
|
||||
return primes
|
||||
}
|
||||
|
||||
function main () {
|
||||
const n = 69 // number till where we wish to find primes
|
||||
const primes = sieveOfEratosthenes(n)
|
||||
for (let i = 2; i <= n; i++) {
|
||||
if (primes[i]) {
|
||||
console.log(i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
Reference in New Issue
Block a user