Primes upto N

Problem Statement

You will be given a number N, print the prime numbers up to N.

Input Format:

An integer N

Output Format:

Print prime numbers up to N with space separation between them.

Constraints:

1 <= N <= 100

Sample Input
Sample Output

Solution

file_type_python primes_upto_n.py
n = int(input())
primes = [True]*(n + 1)
primes[0] = primes[1] = False
for i in range(2, int(n**0.5) + 1):
    if primes[i]:
        for j in range(i**2, n+1, i):
            primes[j] = False

for i in range(2, n + 1):
    if primes[i]:
        print(i, end=" ")

Comments

Load Comments