FastPrepFind All Primes Less Than N

Find All Primes Less Than N

Microsoft logoMicrosoft● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given a nonnegative integer n, return every prime number strictly less than n in increasing order.

Function

primesLessThan(n: int) → int[]

Examples

Example 1

n = 10return = [2,3,5,7]

The primes smaller than 10 are 2, 3, 5, and 7.

Example 2

n = 2return = []

There is no prime strictly smaller than 2.

Constraints

  • 0 <= n <= 5000000

More Microsoft problems

See Microsoft hiring insights
public int[] primesLessThan(int n) {
  // Write your code here.
}
n10
expected[2,3,5,7]
Checking account…