[英]How do I make my python program generate the prime numbers in a certain interval just once instead of them being printed multiple times?
While writing a program to find the prime numbers in a certain range, the code prints every prime number multiple times instead of just listing it once.在编写程序以查找特定范围内的素数时,代码会多次打印每个素数,而不是只列出一次。
This is the code:这是代码:
first_interval = int(input('enter the beginning of the interval'))
second_interval=int(input('enter the end of the interval'))
for digit in range((first_interval),(second_interval+1)):
if digit>1:
for i in range(2,digit):
if (digit %i)==0:
break
else: print(digit)
I was expecting every prime number in the interval (4,21) to be listed just once.我期望区间(4,21)中的每个素数都只列出一次。 What I was expecting:
我期待的是:
7
11
13
17
19
What I got:我得到了什么:
5
5
5
7
7
7
7
7
9
11
11
11
11
11
11
11
11
11
13
13
13
13
13
13
13
13
13
13
13
15
17
17
17
17
17
17
17
17
17
17
17
17
17
17
17
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
21
Instead of printing out digit
on every iteration, use a variable is_prime
and set it to False
whenever you find a divisor of digit
, then print out digit
after the for loop if is_prime
turns out to be True
.不要在每次迭代时都打印出
digit
,而是使用变量is_prime
并在找到digit
的除数时将其设置为False
,然后如果is_prime
结果为True
则在 for 循环后打印出digit
。
first_interval = int(input('enter the beginning of the interval: '))
second_interval = int(input('enter the end of the interval: '))
for digit in range(first_interval, second_interval+1):
if digit > 1:
is_prime = True
for i in range(2, digit):
if (digit % i) == 0:
is_prime = False
break
if is_prime:
print(digit)
Sample input/output:示例输入/输出:
enter the beginning of the interval: 1
enter the end of the interval: 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
When generating a list of prime numbers in a range you first need to ensure that the low value is at least 2. 2 is the lowest and only even prime number and therefore for greatest efficiency should be treated as a special case.在生成一个范围内的素数列表时,首先需要确保低值至少为 2。2 是最低且唯一的偶数素数,因此为了获得最高效率,应将其视为特殊情况。
Once 2 has been dealt with (if relevant) then your loop can increment the test value by 2 because from then on you only need to work with odd numbers.一旦处理了 2(如果相关),那么您的循环可以将测试值增加 2,因为从那时起您只需要处理奇数。
Here's an efficient function that tests for prime numbers followed by a suggestion for how you could implement your code:这是一个高效的 function,它测试质数,然后提供有关如何实现代码的建议:
from math import sqrt, floor
def isprime(n):
if n < 2:
return False
if n == 2 or n == 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
for i in range(5, floor(sqrt(n))+1, 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
start = int(input('enter the beginning of the interval: '))
stop = int(input('enter the end of the interval: '))
# ensure that the start value is at least 2
# if it's exactly 2 just print it
if (start := min(max(2, start), stop)) == 2
print(2)
# ensure that the start value is an odd number
start |= 1
# now work in increments of 2
for n in range(start, stop+1, 2):
if isprime(n):
print(n)
Output: Output:
enter the beginning of the interval: 4
enter the end of the interval: 21
5
7
11
13
17
19
You could use all
to check whether digit is prime or false and print it:您可以使用
all
来检查数字是素数还是假数并打印它:
first_interval = int(input('enter the beginning of the interval : '))
second_interval=int(input('enter the end of the interval : '))
for digit in range(first_interval,second_interval):
if digit > 1 and all(digit % i for i in range(2,digit)):
print(digit)
enter the beginning of the interval : 4
enter the end of the interval : 21
5
7
11
13
17
19
It is a good idea to first write a function that determines if a number is prime or not and then call this function for each number in the desired range:最好先编写一个 function 来确定一个数是否为质数,然后为所需范围内的每个数调用此 function:
def is_prime(number):
if number < 2:
return False
for i in range(2,number):
if number % i == 0:
return False
return True
first_interval = int(input('enter the beginning of the interval: '))
second_interval = int(input('enter the end of the interval: '))
for digit in range(first_interval,second_interval+1):
if is_prime(digit):
print(digit)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.