简体   繁体   English

如何让我的 python 程序在某个时间间隔内只生成一次质数,而不是多次打印它们?

[英]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.

相关问题 如何编写代码以生成随机数,随机数只能每 5 次打印一次 - How do I write code in order to generate random numbers in a way that a random number can only be printed once every 5 times 一旦我在 python 中打印了一定数量的数字,有没有办法停止我的循环? - Is there a way to stop my loop once I have printed a certain amount of numbers in python? 如何找到一定范围内的所有素数并打印出来? - How do i find all the prime numbers in a certain range and print them out? Python-如何使该RNG程序在生成数字一次后继续运行x次 - Python - How do i make this RNG program continue to run for x amount of times after generating the number once 我的素数程序中的指数引发了内存错误,我该如何解决? - An exponent within my prime numbers program throws a memory error, how do I solve this? 当其他功能每天以固定间隔运行多次时,如何每天运行一次 function - How do I run a function once a day when other functions run with fixed interval multiple times a day 写一个Python程序打印一个区间内的所有质数? - Write a Python program to print all prime numbers in an interval? 如何使 Python 中的这个 Tkinter 单选按钮只添加一次选择的字母,而不是添加两次第一个字母? - How do I make this Tkinter Radiobutton in Python only add the letter that's been selected once, instead of adding the first letter two times? 如何多次执行Java / Python程序? - How do I execute a Java/Python program multiple times? 在 python 中打印后如何关闭 pdf 文件? - How do I close a pdf file once it has printed in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM