Created
July 23, 2015 14:01
-
-
Save ihorkatkov/bd39498561544a439142 to your computer and use it in GitHub Desktop.
[JS] Реализация древнего алгоритма Эрастофена
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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