【问题标题】:Print next N prime number打印下一个 N 素数
【发布时间】:2021-08-11 21:44:41
【问题描述】:

这个问题是朋友向我提出的。它是这样的: - 给定两个整数 i 和 n,从 i 开始打印接下来的 n 个素数

注意:- 问题是询问下 n 个素数,但未指定 i 到 n 等范围。

这是我想出的,但不幸的是,它不起作用。你能帮忙吗?

def is_prime(Num):
    prime = True
    if Num > 1:
        for i in range(2, Num):
            if (Num % i) == 0:
                prime = False
        if prime:
            return Num

if __name__ == "__main__":
    startNum = int(input("Enter the first number: "))
    primeNum = int(input("Enter the number of primes you want to print: "))

    primeList = []

    length = len(primeList)

    while length <= primeNum:
        x = is_prime(startNum)
        primeList.append(x)
        startNum = startNum + 1
        length = length + 1

    print(primeList)
    print(x)

输出如下

Enter the first number: 3
Enter the number of primes you want to print: 5
[3, None, 5, None, 7, None]
None

【问题讨论】:

    标签: python arrays primes


    【解决方案1】:

    关闭。您正在将来自is_prime 的每个返回添加到列表中,无论它成功还是失败。用这个替换你的主循环:

        while len(primeList) <= primeNum:
            if is_prime(startNum):
                primeList.append(startNum)
            startNum += 1
    

    【讨论】:

      【解决方案2】:

      问题是is_prime() 只在Num 为素数 时返回一个数字。当输入不是素数时,它不返回任何内容(即None)。

      当您将is_prime() 的结果附加到primeList 时,您将附加一个None 值——如屏幕截图所示。

      我建议更改is_prime(),使其返回TrueFalse,具体取决于prime,然后在if() 语句中使用它来选择是否附加数字和相应地增加length .

      【讨论】:

        【解决方案3】:

        如果要输出的素数非常多,创建惰性素数生成器并添加一些参数来过滤/限制其输出可能会更有效:

        def primes(firstNum=0,count=-1):
            skips = dict() # non-primes to skip
            p,inc = 2,1
            while count:
                if p not in skips:    # p is a new prime (not skipped)
                    skips[p*p] = 2*p  # setup skip value for new prime 
                    if p>=firstNum:   # filter output to >= firstNum
                        yield p
                        count -= 1
                else:                         # multiple of a prime, skip p
                    stride   = skips.pop(p)   # move skip number forward
                    multiple = p + stride  
                    while multiple in skips:  # avoid collisions
                        multiple += stride    
                    skips[multiple] = stride 
                p,inc = p+inc,2               # next potential prime
        

        输出:

        for p in primes(10,5): print(p)
        
        11
        13
        17
        19
        23
        

        惰性素数生成器通过跟踪非素数且应被跳过的数字来工作。每个素数都会产生一个不同的数字来跳过,从而避免与其他素数的冲突。

        如果您不向生成器提供计数,它将永远不会停止生成素数(以每个找到的素数一个字典条目的速度消耗内存)。生成器比埃拉托色尼筛法慢,但不需要知道要获得的第 N 个素数的大小。

        【讨论】:

          【解决方案4】:

          以上作品由 Tim Roberts 提供。

          def is_prime(Num):
              prime = True
              if Num > 1:
                  for i in range(2, Num):
                      if (Num % i) == 0:
                          prime = False
                  if prime:
                      return Num
          
          if __name__ == "__main__":
              startNum = int(input("Enter the first number: "))
              primeNum = int(input("Enter the number of primes you want to print: "))
          
              primeList = []
          
              length = len(primeList)
          
              while len(primeList) <= primeNum:
                  if is_prime(startNum):
                      primeList.append(startNum)
                  startNum += 1
          
              print(primeList)
          

          输出:

          Enter the first number: 1
          Enter the number of primes you want to print: 10
          [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
          

          【讨论】:

            猜你喜欢
            • 2016-02-16
            • 2017-03-06
            • 2012-03-26
            • 2017-05-03
            • 2013-03-26
            • 1970-01-01
            • 2010-12-10
            • 1970-01-01
            • 2012-01-05
            相关资源
            最近更新 更多