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: False0 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ố Show
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?Để 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: False1 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: False0 đế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: False1 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: False1 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: False1) 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: False1 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)
|