Created
November 15, 2012 14:56
-
-
Save Alexey-N-Chernyshov/4079026 to your computer and use it in GitHub Desktop.
Решето Эратосфена на Python.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import math | |
n = input("Вывод простых чисел до числа (включительно): ") + 1 | |
# Выписать подряд все целые числа от 2 до n (2, 3, 4, …, n). | |
a = [True] * n | |
# Берëм невычеркнутые числа от 2 до sqrt(n) | |
for i in xrange(2, int(math.sqrt(n))): | |
# Вычëркиваем числа, кратные невычеркнутому | |
for j in xrange(i * 2, n, i): | |
a[j] = False | |
# Все невычеркнутые числа - простые | |
b = [i for i in xrange(2, n) if a[i]] | |
print(b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
не работает до 8 включительно