Python BasicsEasy

Prime number within a given range

Detailed guide and Python implementation for the 'Prime number within a given range' problem.

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?
Consider using Basics-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.