Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find All Primes in a Range

Basic Acceptance 98.24% Points 10.00

You are given two integers L and R. Your task is to return all prime numbers in the inclusive range [L, R], arranged in ascending order.

A prime number is an integer greater than 1 that has exactly two distinct positive divisors: 1 and itself. If the range contains no prime numbers, return an empty result.

Examples
Example 1

Input: L = 10, R = 20
Output: 11 13 17 19
Explanation: The prime numbers between 10 and 20, inclusive, are 11, 13, 17, and 19.

Example 2

Input: L = 1, R = 5
Output: 2 3 5
Explanation: The prime numbers between 1 and 5, inclusive, are 2, 3, and 5. The number 1 is not prime.

Example 3

Input: L = 14, R = 16
Output: (empty)
Explanation: None of the numbers from 14 through 16 is prime, so the result is empty.

Constraints
  • 1 = L = R = 106
Companies
Paypal DE Shaw
Topics
Data Structures Basics Searching Algorithm Basics
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions