【问题标题】:Runtime Crash For A Very Basic Python Program一个非常基本的 Python 程序的运行时崩溃
【发布时间】:2010-09-02 17:09:05
【问题描述】:

我在装有 Python 2.6 的 Windows XP PC 上工作,我试图解决 Project Euler 问题,但每当我执行代码时,解释器就会挂起。我已经通过 PyScripter、IDLE 和 MonkeyStudio 对其进行了调试,但即使对于像 15 这样的微不足道的值,它仍然无法正常工作。

我只是不明白为什么。你能帮帮我吗?

代码如下:

"""Project Euler Problem 3
Author: A"""

num = 15
prime = [1]
x = long (round(num/2))

def ifprime (x): 
        """ Defining the function that checks if the number is prime or not"""    
        """ Checking if the passed number is prime or not"""

        y = long(round(x/2))
        while y > 0:
                if x%y == 0:
                        return False
                y -= 1
        return True

while x > 0:
    if num%x == 0:
        if ifprime(x):
                print "I've found a prime! "
                print x
                prime[len(prime):] = [x]
        x -= 1

【问题讨论】:

  • 您说您尝试过调试它...您自己找不到问题所在吗? (我做到了,很明显发生了什么)
  • 我不知道为什么我没有看到它。我几天前才开始编码。所以,这可能与它有关。

标签: python crash


【解决方案1】:

您的 x -= 1 语句缩进了一级。

x 只会在 num % x 为 0 时递减

应该是这样的:

while x > 0:
    if num%x == 0:
        if ifprime(x):
                print "I've found a prime! "
                print x
                prime[len(prime):] = [x]
    x -= 1

【讨论】:

    【解决方案2】:

    你有一个无限循环:

    x -= 1 永远不会被调用,因为它在num%x == 0 条件下,永远不会发生(因为x 永远不会改变它的值)。

    num 为 15 时,x 以 7 开头。然后,num % x 为 1,因此条件为假,x 不递减 - 从而无限循环。

    【讨论】:

      【解决方案3】:

      除了其他人指出的,您的ifprime 是错误的。您正在检查while y > 0,当然,它测试到y = 1,因此将始终返回false。

      出于优化目的,您不必测试到x/2,您可以测试到sqrt(x),这就足够了。

      import math
      
      def ifprime (x): 
      
          y = math.ceil(math.sqrt(x))
      
          while y > 1:
              if x % y == 0:
                  return False
              y -= 1
      
          return True
      

      【讨论】:

      • 是的,我在运行时注意到了这一点,并进行了更正。另一方面,如果我将其视为 y>1,则该函数不会将 2 识别为素数。所以,我必须弄清楚这一点。非常感谢您的优化技巧。
      • @JustA:将 2 硬编码为素数,并且仅检查是否可被 2 和奇数整除 >= 3。
      • @Brian:我现在感觉自己像个智障。我有很多东西要学。
      • 还请记住,如果您从 2 点开始并逐步提高,则获得匹配的可能性(对于早期出局)要大得多。
      【解决方案4】:

      我有dealt so much with primes,我做了补码以查找因子并使用它定义 ifprime,以进行更改。

      import math
      
      ## I want any which return first not False value
      def some(seq):
          for item in seq:
              if item:
                  return item
      
      def factor (number):
          """ returns smallest factor of number or None """
          if number < 4: return None
          return some(divisor
                      for divisor in [2] + range(3,int(number ** 0.5)+2, 2)
                      if not(number % divisor))
      
      # little slower way for fun (because factor gives the factoring it found)
      def ifprime(number):
          return number > 1 and not factor(number)
      
      print [number for number in range(100) if ifprime(number)]
      

      (如果您需要多个素数,请使用sieve algorithm 而不是素数测试。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-26
        • 1970-01-01
        • 1970-01-01
        • 2023-03-15
        相关资源
        最近更新 更多