Prime Number

Problem Statement

Given an integer n, print all prime numbers before n.


Input Format:

The input consists of only a single integer n.

Output Format:

Print the first n prime numbers separated by a space.

Constraints:

1 <= n <= 1000

Sample Input
Sample Output

Solution

file_type_python count_of_family_members.py
n = int(input())
sieve = [True] * (n + 1)
sieve[0] = sieve[1] = False
for p in range(2, int(n**0.5) + 1):
    if sieve[p]:
        sieve[p**2 : n+1 : p] = [False] * len(sieve[p**2 : n+1 : p])

res = [p for p, is_prime in enumerate(sieve) if is_prime]
print(*res)

Comments

Load Comments