Created
September 28, 2016 07:30
-
-
Save jtprogru/744aaa597aedf675cfb45fb8d823ae74 to your computer and use it in GitHub Desktop.
Решето Эратосфена на Python3
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 python3 | |
# -*- coding: utf-8 -*- | |
import math | |
n = eval(input("Вывод простых чисел до числа (включительно): ")) + 1 | |
# Выписать подряд все целые числа от 2 до n (2, 3, 4, …, n). | |
a = [True] * n | |
# Берëм невычеркнутые числа от 2 до sqrt(n) | |
for i in range(2, int(math.sqrt(n))): | |
# Вычëркиваем числа, кратные невычеркнутому | |
for j in range(i * 2, n, i): | |
a[j] = False | |
# Все невычеркнутые числа - простые | |
b = [i for i in range(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