Sieve of Eratosthenes
หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
อันนี้สร้างจากบทเรียนที่คุณยังไม่เสร็จสิ้น ให้ทำบทเรียนนั้นก่อน:
Python Basics · Functions →
กรอง eratosthenes
นักคณิตศาสตร์กรีก eratosthenes หาเลขต้นแบบโดยไม่ทำการหาร: เขียนตัวเลขลง แล้วขีดฆ่าตัวคูณของแต่ละเลขต้นแบบ
[True] * (n + 1) สร้างรายการของ n + 1 True values, One for each number from 0 to n. Mark 0 and 1 as not prime. Then, for each p still marked True, set every multiple of p from p * p onwards to False. The numbers still marked True at the end are the primes.
ความยาก 5/5 · 400 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Write primes_up_to(n) that returns every prime number from 2 ไป n in order, using the sieve of Eratosthenes. primes_up_to(10) เป็น [2, 3, 5, 7].
คลิก Run เพื่อดูผลลัพธ์ที่นี่