All prime numbers
Level MEDIUM
Given an integer N, print all the prime numbers that lie in the range 2 to N (both inclusive).
Print the prime numbers in different lines.
Input Format :
Output Format :
Constraints :
Sample Input 1:
Sample Output 1:
Sample Input 2:
Sample Output 2:
This file contains hidden or 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
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
Post a Comment
Please give us your valuable feedback