Python Output Prime Numbers within a Specified Range
Prime numbers are infinite. Apart from 1 and themselves, they are not divisible by any other divisor.
The following example can output prime numbers within a specified range:
Example (Python 3.0+)
#!/usr/bin/python3
# Output prime numbers within the specified range
# take input from the user
lower = int(input("Enter the minimum value of the interval:"))
upper = int(input("Enter the maximum value of the interval:"))
for num in range(lower,upper + 1):
# Prime numbers are greater than 1
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
After executing the above program, the output result is:
$ python3 test.py 输入区间最小值: 1 输入区间最大值: 100 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Using trial division
Trial division is a basic algorithm that performs divisibility tests on each number to determine whether it is prime. The following is code that uses trial division to output prime numbers within a specified range:
Example
def is_prime(n):
"""Determine whether a number is a prime number"""
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
def primes_in_range(start, end):
"""Output prime numbers within the specified range"""
for num in range(start, end + 1):
if is_prime(num):
print(num)
# Example: output prime numbers in the range 10 to 50
primes_in_range(10, 50)
"""Determine whether a number is a prime number"""
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
def primes_in_range(start, end):
"""Output prime numbers within the specified range"""
for num in range(start, end + 1):
if is_prime(num):
print(num)
# Example: output prime numbers in the range 10 to 50
primes_in_range(10, 50)
Using the Sieve of Eratosthenes
The Sieve of Eratosthenes is a more efficient algorithm, especially suitable for finding prime numbers in a large range. The following is code that uses the Sieve of Eratosthenes to output prime numbers within a specified range:
Example
def sieve_of_eratosthenes(max_num):
"""Use the Sieve of Eratosthenes to find all prime numbers up to max_num"""
sieve = [True] * (max_num + 1)
sieve[0] = sieve[1] = False # 0 and 1 are not prime numbers
p = 2
while p * p <= max_num:
if sieve[p]:
for multiple in range(p * p, max_num + 1, p):
sieve[multiple] = False
p += 1
return [p for p, is_prime in enumerate(sieve) if is_prime]
def primes_in_range(start, end):
"""Output prime numbers within the specified range"""
primes = sieve_of_eratosthenes(end)
for num in primes:
if num >= start:
print(num)
# Example: output prime numbers in the range 10 to 50
primes_in_range(10, 50)
"""Use the Sieve of Eratosthenes to find all prime numbers up to max_num"""
sieve = [True] * (max_num + 1)
sieve[0] = sieve[1] = False # 0 and 1 are not prime numbers
p = 2
while p * p <= max_num:
if sieve[p]:
for multiple in range(p * p, max_num + 1, p):
sieve[multiple] = False
p += 1
return [p for p, is_prime in enumerate(sieve) if is_prime]
def primes_in_range(start, end):
"""Output prime numbers within the specified range"""
primes = sieve_of_eratosthenes(end)
for num in primes:
if num >= start:
print(num)
# Example: output prime numbers in the range 10 to 50
primes_in_range(10, 50)
Other extensions
Python3 Examples