Mô phỏng thuật toán Sàng Eratosthenes

Sieve · Nhóm: Mảng

Sàng Eratosthenes (Sieve), nhóm Mảng.

Mã Python

def sieve(n):
    is_p = [True] * (n + 1)
    is_p[0] = is_p[1] = False
    for p in range(2, n + 1):
        if is_p[p]:
            for m in range(p * p, n + 1, p):
                is_p[m] = False
    return [x for x in range(2, n + 1) if is_p[x]]

Mở trang để xem mô phỏng từng bước và xuất slide bài giảng.