0

我今天的问题是,我是否正在为 Euler 145 走上正确的道路,以及它是否有点有效。我已经完成了大部分工作,只有我的一个 Defs 给我带来了麻烦 int(str(numb)[:i])%2==0 进行偶数检查。我的代码如下。第 10 行是问题点

def reversed(reg): # to flip the number around
    fliped = str(reg)[::-1];
    return(int(fliped)); # Return it as a int. 

def allEvenDigits(numb): # This is the issue one
    hasEvenNumb = False;
    for i in range(0, len(str(numb))):
        if int(str(numb)[:i])%2 == 0:  # if int of the string numb's char at i is even
            hasEvenNumb = True; ## return that it is true
            break; # why go on if we found a even. 
    return(hasEvenNumb);


for i in range(1, 1000): # its 1000 to save a few minutes
    revNumb = reversed(i);
    total = revNumb+i;
    if(allEvenDigits(total)):
        print(i, "+" , revNumb, "=",Total);
4

3 回答 3

1

您可以使用内置功能all()并使用集合来跟踪已经解决的数字;例如,如果您已解决36,则没有理由解决63

seen = set()

def allEvenDigits(numb): # This is the issue one
    return all( int(n)%2 == 0 for n in str(numb))

for i in range(1, 1000): # its 1000 to save a few minutes
    revNumb = reversed(i);
    total = revNumb+i;

    if i not in seen and revNumb not in seen:
        if (allEvenDigits(total)):
            print(i, "+" , revNumb, "=",total);
            seen.add(i)
            seen.add(revNumb)

输出:

(1, '+', 1, '=', 2)
(2, '+', 2, '=', 4)
(3, '+', 3, '=', 6)
(4, '+', 4, '=', 8)
(11, '+', 11, '=', 22)
(13, '+', 31, '=', 44)
(15, '+', 51, '=', 66)
(17, '+', 71, '=', 88)
(22, '+', 22, '=', 44)
(24, '+', 42, '=', 66)
(26, '+', 62, '=', 88)
(33, '+', 33, '=', 66)
(35, '+', 53, '=', 88)
(44, '+', 44, '=', 88)
...

帮助all:_

>>> all?
Type:       builtin_function_or_method
String Form:<built-in function all>
Namespace:  Python builtin
Docstring:
all(iterable) -> bool

Return True if bool(x) is True for all values x in the iterable.
If the iterable is empty, return True.
于 2013-05-11T03:50:42.413 回答
0

当您的范围为range(0, len(str(numb))). 您可以通过以下方式解决它:

def allEvenDigits(numb): # This is the issue one
    hasEvenNumb = False;
    for i in range(1, len(str(numb))):
        if int(str(numb)[:i])%2 == 0:  # if int of the string numb's char at i is even
            hasEvenNumb = True; ## return that it is true
            break; # why go on if we found a even. 
    return(hasEvenNumb);

>>> allEvenDigits(52)
False

然而,似乎更容易做的是检查每个数字是否是偶数:

def allEvenDigits(numb):
    hasEvenNumb = True
    for char in str(numb):
        if int(char) % 2 == 0:
            hasEvenNumb = False
            break
    return hasEvenNumb

allEvenDigits(52)

让它更简单一点,并且只检查单个数字而不是子字符串。

于 2013-05-11T03:43:54.957 回答
0
def sumrevers(x):
    summation = x + int(str(x)[::-1])
    if summation % 2 != 0: return summation


def checknum(x):
    if not (str(x)[-1] == "0") or (str(x)[0] == "0"):
        if type(sumrevers(x)) == int:
            num = str(sumrevers(x))
            checklis = [k for k in str(num)]
            if all(int(i) % 2 != 0 for i in checklis): return True


cnt = 0
for i in xrange(1, 1000000001):
    if checknum(i):
        cnt += 1
print cnt
于 2016-02-21T02:09:05.000 回答