为什么这个脚本在某些情况下会陷入无限循环?(Why does this script get stuck in infinite loop for some cases?)

编程入门 行业动态 更新时间:2024-10-20 09:24:11
为什么这个脚本在某些情况下会陷入无限循环?(Why does this script get stuck in infinite loop for some cases?)

我正在重新编写一个旧的python脚本。 在通过一些随机测试用例运行它时,我注意到它会在某些情况下陷入无限循环,但对其他情况则不然。 这个脚本适用于Project Euler Problem 3(适用于问题提示,因此从未注意到随机无限循环)。 这将适用于10,19,51,600851475143。它陷入无限循环152.我没有尝试过其他人,但认为这是足够的测试用例来注意到“奇怪”的东西。

这是代码:

import sys def largestPrime(n): largest_prime = 0 # initialize largest prime d = 2 # set first value for factor evaluation while n > 1: # n will be divided by each factor later on while n % d == 0: # check if n is divisible by factor if d > largest_prime: # check if d is greater than largest_prime largest_prime = d # if so, set largest_prime = d n /= d # if so, can divide n by d to find remaining factors d += 1 return largest_prime def main(): # Make a list of command line arguments, omitting the [0] element # which is the script itself. args = sys.argv[1:] if not args: #if list is empty; return message & exit print ("Usage: euler003.py 'n'") sys.exit(1) if len(args) > 1: #if list less than 1; return message & exit print ("You've entered too many arguments; Usage: euler001.py 'n'") sys.exit(1) largest = largestPrime(int(args[0])) print (largest) # This is the standard boilerplate that calls the main() function. if __name__ == "__main__": main()

I'm re-working an old python script. While running it through some random test cases, I'm noticing that it will get stuck in an infinite loop for some cases, but not for others. This script is for Project Euler Problem 3 (works for question prompt, so never noticed random infinite loops). This will work for 10, 19, 51, 600851475143. It gets stuck in an infinite loop for 152. I haven't tried others, but thought this was enough test cases to notice something 'odd'.

Here's the code:

import sys def largestPrime(n): largest_prime = 0 # initialize largest prime d = 2 # set first value for factor evaluation while n > 1: # n will be divided by each factor later on while n % d == 0: # check if n is divisible by factor if d > largest_prime: # check if d is greater than largest_prime largest_prime = d # if so, set largest_prime = d n /= d # if so, can divide n by d to find remaining factors d += 1 return largest_prime def main(): # Make a list of command line arguments, omitting the [0] element # which is the script itself. args = sys.argv[1:] if not args: #if list is empty; return message & exit print ("Usage: euler003.py 'n'") sys.exit(1) if len(args) > 1: #if list less than 1; return message & exit print ("You've entered too many arguments; Usage: euler001.py 'n'") sys.exit(1) largest = largestPrime(int(args[0])) print (largest) # This is the standard boilerplate that calls the main() function. if __name__ == "__main__": main()

最满意答案

将n/=d置于def largestPrime(n): if (但在while内def largestPrime(n):因为在代码中,只有当d大于largest_prime时才会执行n/=d ,这是错误的。 必须进行n/=d直到n % d == 0

while n % d == 0: if d > largest_prime: largest_prime = d n /= d d += 1

Placen/=d outside the if (but inside the while) in def largestPrime(n): Because in your code, you do n/=d only when d is greater than largest_prime which is wrong. n/=d must be done till n % d == 0

while n % d == 0: if d > largest_prime: largest_prime = d n /= d d += 1

更多推荐

本文发布于:2023-07-17 13:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1144956.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   情况下   在某些   script   cases

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!