Cách tìm số nguyên tố hiệu quả trong python

Số nguyên tố là số tự nhiên [lớn hơn 1] có đúng hai ước là

def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
0 và chính nó. Để kiểm tra một số có phải là số nguyên tố hay không ta có thể đếm số thừa số. Nếu là 2 thì ta nói số đó là số nguyên tố, ngược lại là hợp số. Lưu ý, các số không nguyên tố được gọi là hợp số

Mục lục

  • Làm thế nào để kiểm tra xem một số có phải là số nguyên tố hay không?
  • Thực hiện Brute Force Python để kiểm tra xem một số có phải là số nguyên tố hay không - O[N]
  • Phương thức O[sqrt[N]] để kiểm tra xem một số có phải là số nguyên tố hay không
    • Triển khai Python - O[sqrt[N]]
    • Triển khai C++ - O[sqrt[N]]
  • Sàng Eratosthenes
    • Python triển khai Sàng của Eratosthenes
    • Triển khai C++ Sàng của Eratosthenes

Làm thế nào để kiểm tra xem một số có phải là số nguyên tố hay không?

Để kiểm tra xem một số có phải là số nguyên tố hay không, chúng ta đếm thừa số [hoặc ước] của nó. Nếu số đếm là 2 thì đó là số nguyên tố. Vì vậy, có vẻ như bài toán kiểm tra tính nguyên tố cũng khó như việc tìm thừa số của một số. Tuy nhiên với số nguyên tố ta không cần tìm hết các thừa số mà chỉ cần tính

Phương pháp brute force để kiểm tra xem

def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
1 có phải là số nguyên tố hay không sẽ là lặp lại từ
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
0 đến
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
1 và kiểm tra xem có số nào chia hết cho
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
1 không. Nếu số đếm chính xác là 2 [_______00 và chính
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
1] thì
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
1 là số nguyên tố

Thực hiện Brute Force Python để kiểm tra xem một số có phải là số nguyên tố hay không - O[N]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False

Độ phức tạp về thời gian của phương pháp trên là O[N],

def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
8 là số đang được kiểm tra tính nguyên tố. Vì vậy, trong trường hợp số của chúng tôi là 1000000000, thì phương thức này không thể trả về trong thời gian khả thi. Làm thế nào chúng ta có thể tối ưu hóa nó?

Phương thức O[sqrt[N]] để kiểm tra xem một số có phải là số nguyên tố hay không

Trong khi tìm các thừa số của một số, chúng ta thấy rằng chỉ cần lặp từ

def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
0 đến
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
22 là đủ để tìm tất cả các thừa số của
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
8. Vì vậy, từ
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
0 đến
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
22, chúng tôi sẽ tìm thấy chính xác 1 yếu tố, i. e.
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
0 chính nó. Hãy lặp lại từ
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
27 đến
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
22. Chúng tôi sẽ không tìm thấy bất kỳ yếu tố nào trong phạm vi này. Vậy nếu tìm được thừa số nào trong khoảng này thì ta gọi số đó là số không nguyên tố, ngược lại là số nguyên tố

Triển khai Python - O[sqrt[N]]

def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
2
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
2

Triển khai C++ - O[sqrt[N]]

def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
3____24

Độ phức tạp về thời gian của thuật toán trên là O[sqrt[N]]. Hãy tiếp tục, hãy thử kiểm tra một số lớn như 1000000000, việc triển khai này sẽ trả về kết quả trong nháy mắt. Tuy nhiên, đôi khi, chúng ta có thể cần tìm các số nguyên tố trong một phạm vi nhất định. Giả sử phạm vi là

def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
29 đến
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
20. Trong trường hợp này, chúng tôi lặp từ
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
29 đến
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
20 và với mỗi số, chúng tôi kiểm tra xem nó có phải là số nguyên tố hay không trong O[sqrt[N]]. Vì vậy, về tổng thể, chúng ta sẽ có một giải pháp với độ phức tạp về thời gian O[[j-i] * sqrt[N]]. Chúng ta có thể làm gì tốt hơn không?

Sàng Eratosthenes

Ý tưởng đằng sau Sàng của Eratosthenes là gạch bỏ tất cả các vật liệu tổng hợp trong phạm vi nhất định. Khi chúng tôi chắc chắn rằng tất cả các hợp số bị gạch bỏ, chúng tôi còn lại tất cả các số nguyên tố trong phạm vi đã cho. Làm thế nào để chúng ta gạch bỏ tất cả các hợp chất?

Hãy lập quy trình tìm tất cả các số nguyên tố trong khoảng từ

def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
0 đến
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
8

  • Lặp lại từ
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    27 đến
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    22, gọi nó là
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    29
  • Đối với mỗi
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    29, chúng tôi gạch bỏ tất cả các bội số của
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    29, i. e.
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    30,
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    31. và như thế
  • Nếu
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    29 đã bị gạch bỏ, chúng tôi không làm gì cả, vì nếu
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    29 đã bị gạch bỏ, chúng tôi được đảm bảo rằng tất cả các bội số của
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    29 đã bị gạch bỏ trong các lần lặp trước của chúng tôi
  • Cuối cùng, chúng tôi in ra tất cả các số không bị gạch bỏ

Thí dụ. Hãy lấy một phạm vi [2, 25] và tìm tất cả các số nguyên tố trong danh sách này

[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]

  • Lặp lại từ
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    27 đến
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    36, gọi nó là
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    29
    • Vì vậy, chúng tôi lặp lại từ
      def is_prime[n]:
      
          """
          Assumes that n is a positive natural number
          """
          # We know 1 is not a prime number
          if n == 1:
              return False
      
          # We store the number of factors in this variable
          factors = 0
          # This will loop from 1 to n
          for i in xrange[1, n+1]:
              # Check if `i` divides `n`, if yes then we increment the factors
              if n % i == 0:
                  factors += 1
          # If total factors are exactly 2
          if factors == 2:
              return True
          return False
      
      # Test the above function
      for x in xrange[1, 11]:
          print '%d: %s' % [x, is_prime[x]]
      
      # Output:
      # 1: False
      # 2: True
      # 3: True
      # 4: False
      # 5: True
      # 6: False
      # 7: True
      # 8: False
      # 9: False
      # 10: False
      
      27 đến
      def is_prime[n]:
      
          """
          Assumes that n is a positive natural number
          """
          # We know 1 is not a prime number
          if n == 1:
              return False
      
          # We store the number of factors in this variable
          factors = 0
          # This will loop from 1 to n
          for i in xrange[1, n+1]:
              # Check if `i` divides `n`, if yes then we increment the factors
              if n % i == 0:
                  factors += 1
          # If total factors are exactly 2
          if factors == 2:
              return True
          return False
      
      # Test the above function
      for x in xrange[1, 11]:
          print '%d: %s' % [x, is_prime[x]]
      
      # Output:
      # 1: False
      # 2: True
      # 3: True
      # 4: False
      # 5: True
      # 6: False
      # 7: True
      # 8: False
      # 9: False
      # 10: False
      
      39
  • Đối với 2, vượt qua tất cả các bội số của 2 i. e.
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    40,
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    41,
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    42. và như thế

[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]

  • Đối với 3, vượt qua tất cả các bội số của 3 i. e.
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    43,
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    44,
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    45. và như thế

[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]

  • Đối với 4, Ohh. Bản thân 4 đã bị gạch bỏ, vì vậy chúng tôi đảm bảo rằng tất cả các bội số của nó cũng bị gạch bỏ

  • Đối với 5, gạch chéo tất cả các bội số của 3 i. e.

    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    46,
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    47,
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    48. và như thế

[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]

Bây giờ, tất cả các số không bị gạch bỏ đều là số nguyên tố trong khoảng [2, 25]. họ đang. [2, 3, 5, 7, 11, 13, 17, 19, 23]

Một hình ảnh đẹp của quá trình trên đã được tìm thấy trên wikipedia2

Hãy chuyển đổi quy trình trên thành mã

Python triển khai Sàng của Eratosthenes

def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
5
def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
6

Triển khai C++ Sàng của Eratosthenes

def is_prime[n]:

    """
    Assumes that n is a positive natural number
    """
    # We know 1 is not a prime number
    if n == 1:
        return False

    # We store the number of factors in this variable
    factors = 0
    # This will loop from 1 to n
    for i in xrange[1, n+1]:
        # Check if `i` divides `n`, if yes then we increment the factors
        if n % i == 0:
            factors += 1
    # If total factors are exactly 2
    if factors == 2:
        return True
    return False

# Test the above function
for x in xrange[1, 11]:
    print '%d: %s' % [x, is_prime[x]]

# Output:
# 1: False
# 2: True
# 3: True
# 4: False
# 5: True
# 6: False
# 7: True
# 8: False
# 9: False
# 10: False
3____58

Phân tích độ phức tạp

  • Độ phức tạp không gian. Chúng tôi sử dụng không gian O[N] để khởi tạo mảng
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    49
  • Thời gian phức tạp. Từ tham khảo paper1, vòng lặp đầu tiên lặp lại từ
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    27 đến
    def is_prime[n]:
    
        """
        Assumes that n is a positive natural number
        """
        # We know 1 is not a prime number
        if n == 1:
            return False
    
        # We store the number of factors in this variable
        factors = 0
        # This will loop from 1 to n
        for i in xrange[1, n+1]:
            # Check if `i` divides `n`, if yes then we increment the factors
            if n % i == 0:
                factors += 1
        # If total factors are exactly 2
        if factors == 2:
            return True
        return False
    
    # Test the above function
    for x in xrange[1, 11]:
        print '%d: %s' % [x, is_prime[x]]
    
    # Output:
    # 1: False
    # 2: True
    # 3: True
    # 4: False
    # 5: True
    # 6: False
    # 7: True
    # 8: False
    # 9: False
    # 10: False
    
    22, do đó, nó nhiều nhất là O[sqrt[N]]. Và thời gian dành cho việc loại bỏ bội số nhiều nhất là

Do đó, giới hạn trên tổng thể cho độ phức tạp thời gian hóa ra là O[N log log N]. Điều này thoạt nhìn hơi khó hiểu, tuy nhiên, với việc thực hành phân tích, cuối cùng chúng ta sẽ cảm thấy thoải mái với những bằng chứng và dẫn xuất BigO như vậy

Chủ Đề