Explanation

484.List All Prime Numbers up to N

Easy
Given a non-negative integer n, return every prime number from 2 up to and including n, in increasing order. The trial divisionTrial Division per CandidateTesting each candidate number independently with the same up-to-√i divisibility check used in Check if a Number Is Prime. approach simply repeats the single-number primality check for every candidate from 2 to n. The Sieve of EratosthenesSieve of EratosthenesMarking every multiple of each prime found as composite, so later candidates can be checked with a single array lookup instead of their own division loop. flips the problem around: instead of asking "is this one prime?" over and over, it marks off every multiple of each prime as it's found, so by the end, anything never marked simply must be prime.

Example 1:

Input: n = 10

Output: [2,3,5,7]

Example 2:

Input: n = 1

Output: []

Example 3:

Input: n = 2

Output: [2]

+ 3 hidden test cases run on Submit.

Constraints:

  • ●0 ≤ n ≤ 1000

n =

10