简体   繁体   English

While循环具有多个条件。 蟒蛇

[英]While loops with multiple condtions. Python

I'm writing a function that, given a random int, will return the next int that is both a prime number and a palindrome. 我正在编写一个函数,给定一个随机整数,该函数将返回既是质数又是回文的下一个整数。 ie getint(13) will return 101. One of the conditions of this function is that recursion is not allowed. 即getint(13)将返回101。此函数的条件之一是不允许递归。

Where am i going wrong here? 我在哪里错了?

def golf(number):
    x = number +1
    for i in range(2, x):
        while str(x) != str(x)[::-1] and  x % i == 0:
            x += 1
    return x

Your for loop will only go as far as x 's initial value: the range(2,x) is computed only once, right after x = number + 1 . 您的for循环只会达到x的初始值: range(2,x)仅在x = number + 1之后才计算一次。 So increasing x inside the loop doesn't make a difference. 因此,在循环增加x不会造成任何影响。 Consider using a while loop, something like: 考虑使用while循环,例如:

i = 2
while i <= x:
    ...
    i += 1

And you should probably first check is x is a palindrome (a "cheap" operation) and then, if it is, check weather it is prime (an "expensive" operation), not try do it all in a couple of nested loops that make little sense. 您可能应该首先检查x是否是回文(“便宜”操作),然后检查是否是主要天气(“昂贵”操作),而不要尝试通过几个嵌套循环来完成毫无意义。

Divide the problem into smaller pieces. 将问题分为几个小部分。 It will be easier for you to understand what's going on: 您将更容易了解正在发生的事情:

def golf(number):
    x = number + 1
    while True:
        if is_palindrome(x) and is_prime(x):
            return x
        x += 1

Now all you have to do is implement is_palindrome(x) and is_prime(x) : 现在您要做的就是实现is_palindrome(x)is_prime(x)

import math

def is_prime(x):
    for i in xrange(2, math.ceil(math.sqrt(x))+1):
        if x % i == 0:
            return False
    return True

def is_palindrome(x):
    x = str(x)
    return x == x[::-1]

Side note : the math.ceil(math.sqrt(x))+1 might be an overkill (perhaps int(...)+1 is enough) if math.sqrt works correctly for squares (ie does not depend on floating point calculations so math.sqrt(a*a)==a is always true). 旁注math.ceil(math.sqrt(x))+1可能是一个过大的杀伤力(也许int(...)+1足够了),如果math.sqrt对于正方形可以正常工作(即不依赖于浮点数)计算,因此math.sqrt(a*a)==a始终为true)。 Well in worst case the loop will do one more iteration then necessary. 在最坏的情况下,循环将再执行一次必要的迭代。 Better safe then sorry. 更好的安全后悔。

In your code: 在您的代码中:

def golf(number):
    x = number +1
    for i in range(2, x):
        while str(x) != str(x)[::-1] and  x % i == 0:
            x += 1
    return x

The program execution will not satisfy the while loop half of the time since any even number will not be divisible by any odd number and vice-versa . 程序执行将不能满足while循环一半的时间,因为任何偶数都不能被任何奇数整除, 反之亦然 Also in a single iteration of the for loop, x will be incremented every time the while loop is satisfied. 同样在for循环的单次迭代中,每当while循环满足时, x就会增加。

You need to separate the prime checking and palindrome checking. 您需要将主要检查和回文检查分开。 The approach given by freakish is the simplest. 怪异的方法是最简单的。

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

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