Skip to content

Instantly share code, notes, and snippets.

@pknowledge
Created June 18, 2020 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pknowledge/072e64fd7ebc8cec5162d0907c054e22 to your computer and use it in GitHub Desktop.
Save pknowledge/072e64fd7ebc8cec5162d0907c054e22 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes | Competitive Programming with Python https://youtu.be/-wIYdmPiHcA
from math import *
def genprimes(n):
primes = [True]*(n+1)
primes[0] = False
primes[1] = False
for p in range(2,int(sqrt(n))+1):
if primes[p] == True:
for i in range(p*p,n+1,p):
primes[i] = False
for i in range(0,len(primes)):
if primes[i] == True:
print(i,end=" ")
t = int(input())
while t:
n = int(input())
genprimes(n)
t=t-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment