Print Prime Numbers from 2 to N

 Coding Problem Keys 

 Print Prime Numbers from 2 to N 

 Problem Statement 

The program accepts an integers N as the input. The program must print all the prime numbers from 2 to N (inclusive of N) as the output.

 Input Format 

The first line contains the value of N.

 Output Format 

The first line contains all the prime numbers from 2 to N.

 Boundary Condition(s) 

2 <= N <= 999999

 Example Input/Output 1 

 Input 

11

 Output 

2 3 5 7 11

 Example Input/Output 2 

 Input 

120

 Output 

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113

 Max Execution Time Limit: 3000 millisecs 

 Solution 

 Programming Language: Python 3 Language 

n=int(input())
l=[True for i in range(n+1)]
j=2
while(j*j<=n):
    if l[j]==True:
        for i in range(j*j,n+1,j):l[i]=False
    j+=1
for j in range(2,n+1):
    if l[j]:print(j,end=' ')

# Published By PKJCODERS

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Array Elements - Divisible by D

Move Hundreds to End

Function printArrayElementsInReverse

Unique Characters

Most Vowels in Row

Mixed Matrix - Integers & Characters