Tất cả thử thách
Trung bình 0 đã giải Tỷ lệ chấp nhận 0%

List Primes With a Sieve

Find primes up to a limit by marking composite multiples.

Đề bài

Return all prime numbers from 2 through limit. A prime is an integer greater than 1 divisible only by 1 and itself. Use the sieve idea: keep the next unmarked number and mark its larger multiples as composite. List results in ascending order.

Implement sieve_primes(limit).

Input

limit: The largest number that may be included.

Output

Return the primes in ascending order, or an empty list if none exist.

Ràng buộc

0 ≤ limit ≤ 100,000. The upper bound is inclusive. Output tests verify results, not the exact algorithm used.

Ví dụ

Ví dụ 1
Đầu vào:
limit = 12

Kết quả mong đợi:
expected = [2, 3, 5, 7, 11]
Đầu vào:
long long limit = 12;

Kết quả mong đợi:
std::vector<long long> expected = {2, 3, 5, 7, 11};
Đầu vào:
long limit = 12;

Kết quả mong đợi:
long[] expected = new long[] {2, 3, 5, 7, 11};

Giải thích

Mark multiples of 2 and 3 as composite. The remaining values up to 12 are 2, 3, 5, 7, and 11.

Ví dụ 2
Đầu vào:
limit = 1

Kết quả mong đợi:
expected = []
Đầu vào:
long long limit = 1;

Kết quả mong đợi:
std::vector<long long> expected = {};
Đầu vào:
long limit = 1;

Kết quả mong đợi:
long[] expected = new long[] {};

Giải thích

Prime numbers begin at 2, beyond this limit.

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.