ปานกลาง 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

List Primes With a Sieve

Find primes up to a limit by marking composite multiples.

โจทย์

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.

เงื่อนไข

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

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
limit = 12

ผลลัพธ์ที่คาดหวัง:
expected = [2, 3, 5, 7, 11]
อินพุต:
long long limit = 12;

ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {2, 3, 5, 7, 11};
อินพุต:
long limit = 12;

ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {2, 3, 5, 7, 11};

คำอธิบาย

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

ตัวอย่างที่ 2
อินพุต:
limit = 1

ผลลัพธ์ที่คาดหวัง:
expected = []
อินพุต:
long long limit = 1;

ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {};
อินพุต:
long limit = 1;

ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {};

คำอธิบาย

Prime numbers begin at 2, beyond this limit.

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก