【问题标题】:The prime function is not returning correct result主要功能没有返回正确的结果
【发布时间】:2019-09-24 05:25:35
【问题描述】:

我写了一段代码来检查一个数字是否是素数。它可以在Powershell中运行,但不能在在线提交平台上运行。

我重新阅读了如何定义一个数字是否为素数,但我找不到我在代码中可能遗漏的任何其他内容。

x = int(input('Please enter a number: '))

if x > 1:
    for i in range(2, x):
        if (x % i) == 0:
            print('The number you inputted is not a prime number.')
            break
        else:
            print('The number you inputted is a prime number.')
            break
else:
    print('The number you inputted is not a prime number.')

应该打印出一个数字是否是素数。

【问题讨论】:

  • 循环内的条件总是在第一次迭代时中断循环。从解决这个问题开始。
  • 无法在网上投稿平台上使用。请提供更多详情。您收到的错误消息或错误输出是什么
  • 这段代码根本不起作用。在尝试所有可能的质因数之前,您无法知道一个数字是否为质数。不只是其中之一。
  • 另外,您最多可以检查 x 的平方根而不是 x - 1
  • @SayandipDutta 是的,他确实需要一个循环。你的观点?

标签: python primes


【解决方案1】:

您可以使用for-else 构造,以便仅在循环结束时才确定素数,而不会因找到除数而中断。此外,在查找除数时,您只需要迭代到输入数字的平方根:

x = int(input('Please enter a number: '))

for i in range(2, int(x ** .5) + 1):
    if x % i == 0:
        print('The number you inputted is not a prime number.')
        break
else:
    print('The number you inputted is a prime number.')

【讨论】:

    【解决方案2】:

    嗯,首先你必须删除最里面的 else 子句。

    以 x = 9 为例。控制流将进入 for 子句,从 i=2 开始。它将测试 x % i == 0,即 x 是否可被 i 整除。它不是。然后转到 else 子句。打印“您输入的数字是质数”并中断,结束 for 循环。这不是你想要的。

    您只有在查看它是否可以被所有 i 整除后才能得出结论,该数字是素数。也就是说,您希望得出数字为素数的代码(print("The number you input is a prime number"))位于 for 循环之外。

    总体上需要进行一些调整。这是一个可行的解决方案:

    x = int(input('Please enter a number: '))
    
    if x > 1:
        for i in range(2, x):
            if (x % i) == 0:
                print('The number you inputted is not a prime number.')
                break
        if i==x-1:
                print('The number you inputted is a prime number.')
    else:
        print('The number you inputted is not a prime number.'
    

    【讨论】:

      【解决方案3】:
      x = int(input('Please enter a number: '))
      
      if x > 1:
          for i in range(2, x):
              if (x % i) == 0:
                  print('The number you inputted is not a prime number.')
                  break
          else:
              print('The number you inputted is a prime number.')
      
      else:
          print('The number you inputted is not a prime number.')
      

      这个修正奏效了!

      【讨论】:

      • 你不需要if x > 1:,因为如果给定的输入数字不大于2range 生成器将不会生成任何内容,因此会强制循环立即完成并继续进入else 块以确定它是一个素数。
      【解决方案4】:

      解决方案

      这是一个仿照您尝试代码的方式的解决方案。请注意,有时,如果您从某些网站复制代码,您可能会遇到代码格式问题/缩进问题。例如,当我从programiz.com: prime-number 复制一个类似的代码块时,它反复返回缩进错误。我可以解决的唯一方法是手动输入代码逻辑。

      也许可以尝试通过将代码复制粘贴到纯文本编辑器(如记事本)中,然后将其复制粘贴到您正在使用的 IDE 中来解决此问题。

      def is_prime(num):
      
          isprime = False
          # prime numbers are greater than 1
          if (num>1):
              for i in range(2,num):
                  # check if divisible by any number
                  # other than itself and 1
                  if (num % i) == 0:
                      isprime = False
                      break
                  # Not divisible by any number 
                  # other than itself and 1
                  else:
                      isprime = True
          # numbers 0 and 1 are not prime numbers
          else:
              isprime = False
      
          return isprime
      
      is_prime(31), is_prime(42)
      

      输出

      (True, False)
      

      【讨论】:

        【解决方案5】:

        我想建议一种更pythonic的方式:

        x = int(input('Please enter a number: '))
        if x<2:
            print('The number you inputted is not a prime number.')
        elif x==2:
            print('The number you inputted is a prime number.')
        else:
            y = range(2,x)
            mod = map(lambda a:divmod(x,a)[1],y)
            if all(mod):
                print('The number you inputted is a prime number.')
            else:
                print('The number you inputted is not a prime number.')
        

        【讨论】:

          【解决方案6】:

          删除 else 语句怎么样。您确实需要检查所有数字 2 ... (k-1)

          x = int(input('Please enter a number: '))
          
          if x > 1:
              is_prime = True
              for i in range(2, x - 1):
                  if (x % i) == 0:
                      is_prime = False
                      break
              if is_prime:
                  print('The number you inputted is a prime number.')
              else:
                  print('The number you inputted is not a prime number.')
          
          else:
              print('The number you inputted is not a prime number.')
          

          【讨论】:

          • 它将打印 ... not a prime number 后跟 prime number 用于非质数
          猜你喜欢
          • 2023-03-14
          • 1970-01-01
          • 2014-09-13
          • 2012-08-01
          • 2014-05-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多