Back to Practice Dashboard
Python BasicsEasy
Prime number within a given range
Learn how to solve the 'Prime number within a given range' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
Write a function primes_in_range(a, b) that takes two integers a and b (where a <= b) and returns a list of all prime numbers in the range [a, b] inclusive, in ascending order.
Constraints
- •1 <= a <= b <= 10^4
Examples
Example 1
Input
a = 10, b = 30
Output
[11, 13, 17, 19, 23, 29]
Explanation
The prime numbers between 10 and 30 are 11, 13, 17, 19, 23, and 29.
Example 2
Input
a = 1, b = 10
Output
[2, 3, 5, 7]
Explanation
The prime numbers between 1 and 10 are 2, 3, 5, and 7.
Example 3
Input
a = 4, b = 4
Output
[]
Explanation
4 is not prime, so the result is an empty list.
Need a Hint?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.