简体   繁体   English

更改脚本以停止返回非素数并仅返回素数

[英]Change script to stop returning non-prime numbers and only return primes

I have the below script that returns each number stating whether it is prime or not and asking the user if they wish to see the next number in sequence.我有下面的脚本,它返回每个数字,说明它是否是素数,并询问用户是否希望按顺序查看下一个数字。 I wish to make a change so that the script does not produce a print statement for the non-prime numbers and instead skips them and only returns the next prime number, after which asks if the user wished to see the next prime.我希望进行更改,以便脚本不会为非素数生成打印语句,而是跳过它们并仅返回下一个素数,然后询问用户是否希望查看下一个素数。

Here is my code:这是我的代码:

def prime_number():
    game_on = True
    count = 3
    while True:
        for num in range(2, count):
            if (count % num) == 0:
                print(count,"is not a prime number")
                break
        else:
            print(count,"is a prime number")
        question = input("Would you like to see another prime number?? Please enter Yes or No: ")
        if question[0].lower() == "y":
            count = count + 1
            continue
        return

When I run this and select Yes a few times I get the below.当我运行这个和 select 是几次我得到以下。 I want to skip the non-prime numbers altogether and get rid of the non-prime print statements below.我想完全跳过非质数并摆脱下面的非质数打印语句。 I have tried a few ways of ignoring the non-primes in the above while loop but non have worked so far.我尝试了几种方法来忽略上述while循环中的非素数,但到目前为止都没有奏效。

3 is a prime number
Would you like to see another prime number?? Please enter Yes or No: Yes
4 is not a prime number
Would you like to see another prime number?? Please enter Yes or No: Yes
5 is a prime number
Would you like to see another prime number?? Please enter Yes or No: Yes
6 is not a prime number
Would you like to see another prime number?? Please enter Yes or No:

Have you tried creating a list then looping through your for in range loop adding each prime to the list then loop through your list printing all primes to a Given Number.您是否尝试过创建一个列表,然后循环遍历您的 for in range 循环,将每个素数添加到列表中,然后循环遍历您的列表,将所有素数打印到给定数字。

Oh, I get it.哦,我明白了。 You can just do this:你可以这样做:

def next_prime(n):
    while True:
        for denom in range(2, n//2):
            if n % denom == 0:
                n += 1
                break
        else:
            return n

def prime_number():
    game_on = True
    p = 2
    while True:
        p = next_prime(p + 1)
        print(p)
        question = input("Would you like to see another prime number?? Please enter Yes or No: ")
        if question[0].lower() == "y":
            continue
        return

Another proposal close to yours:另一个接近你的建议:

def prime_number():
    number = 3
    while True:
        if all(number % num != 0 for num in range(2, number)):
            print(f"{number} is a prime number")
            response = input("Would you like to see another prime number?? Please enter Yes or No: ")
            if response[0].lower() == "n":
                return
        number += 1

Remarks:评论:

  1. Why starting with 3 ?为什么从3开始? 2 is the first prime. 2是第一个素数。
  2. There are way more efficient ways to compute primes (eg, you could check only up to sqrt(number) ), but I didn't want to change your algo.有更有效的方法来计算素数(例如,你最多只能检查sqrt(number) ),但我不想改变你的算法。 I doubt it really matters for this use case.我怀疑这对这个用例真的很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM