All prime numbers

Level MEDIUM

Given an integer N, print all the prime numbers that lie in the range 2 to N (both inclusive).

Input Format :
Integer N
Output Format :
Prime numbers in different lines
Constraints :
1 <= N <= 100
Sample Input 1:
9
Sample Output 1:
2
3
5
7
Sample Input 2:
20
Sample Output 2:
2
3
5
7
11
13
17
19

l=int(input())
p=2 #set the starting node
prime = [True for i in range(l + 1)] #create a prime list and make all elements set to TRUE
prime[0] = False
prime[1] = False
while p*p <=l:
if prime[p]==True:
for i in range(p*2,l+1,p):
prime[i]=False
p=p+1
"""
This loop basically for printing prime numbers
"""
for p in range(l+1):
if prime[p]:
print(p)

Comments

Popular posts from this blog

MySQL Multi Source Master Slave Replication using GTID

Access and modify all the resources of our Wiki.js using WikiJS API

How to setup an Nginx reverse proxy with a SSL certificate in XWIKI