Skip to content

Instantly share code, notes, and snippets.

@i386net
Last active March 21, 2020 09:40
Show Gist options
  • Save i386net/61486e8abcfaca2a41d9d52dead5e94a to your computer and use it in GitHub Desktop.
Save i386net/61486e8abcfaca2a41d9d52dead5e94a to your computer and use it in GitHub Desktop.
решето Эратосфена
// let ticks = null;
function primes(n) {
const a = new Array(n);
const ret = [];
a.fill(true);
// ticks = 0;
for (let i = 2; i <= n; i++) {
// ticks++;
if (a[i]) {
ret.push(i);
for (let j = i*i; j < n; j+=i) {
a[j] = false;
// ticks++;
}
}
}
// console.log('TICKS: ', ticks);
return ret;
}
let ticks = null;
function primes(n) {
const a = new Array(n);
const ret = [];
a.fill(true);
ticks = 0;
for (let i = 2; i <= n; i++) {
ticks++;
if (a[i]) {
ret.push(i);
for (let j = i*i; j < n; j+=i) {
a[j] = false;
ticks++;
}
}
}
console.log('TICKS: ', ticks);
return ret;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment