Skip to content

Instantly share code, notes, and snippets.

@ihorkatkov
Created July 23, 2015 14:01
Show Gist options
  • Save ihorkatkov/bd39498561544a439142 to your computer and use it in GitHub Desktop.
Save ihorkatkov/bd39498561544a439142 to your computer and use it in GitHub Desktop.
[JS] Реализация древнего алгоритма Эрастофена
function eratosfen(maxNubm) {
var arr = [];
var p = 2;
//Создаем массив заданной длины
for (var i = 2; i < maxNubm; i++) {
arr[i] = true
}
while (p * p < maxNubm) {
// Производим "зачеркивание" чисел по "p"
for (i = 2 * p; i < maxNubm; i += p) {
arr[i] = false;
}
// Определяем следующиее "незачеркнутое" число
for (i = p + 1; i < maxNubm; i++) {
if (arr[i]) break;
}
p = i;
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment