#C12243. Prime Number Filter and Range Query
Prime Number Filter and Range Query
Prime Number Filter and Range Query
This problem requires implementing key functionalities related to prime numbers. You are to write a program that supports three operations: checking if a number is prime, filtering a list to contain only prime numbers, and listing all prime numbers within a specified range.
Functions to Implement:
is_prime(n)
: Returns \(True\) if \(n\) is a prime number, otherwise \(False\).filter_primes(numbers)
: Given a list of integers, returns a new list containing only the prime numbers.range_primes(start, end)
: Returns a list of all prime numbers in the range \([start, end]\) (inclusive).
Your program must read input from stdin and output the results to stdout for each query.
inputFormat
The first line of input contains a single integer \(T\), denoting the number of queries.
Each of the following \(T\) lines represents a query in one of the following formats:
is_prime n
: Check whether \(n\) is a prime number.filter_primes k num1 num2 ... numk
: Given \(k\) integers, output only the prime numbers among them.range_primes start end
: Output all prime numbers in the inclusive range \([start, end]\).
outputFormat
For each query, output the result on a separate line:
- For
is_prime
: output \(True\) or \(False\) (case-sensitive). - For
filter_primes
andrange_primes
: output the list of prime numbers separated by a single space. If no primes are found, output an empty line.
3
is_prime 17
filter_primes 5 2 4 6 7 11
range_primes 10 20
True
2 7 11
11 13 17 19
</p>