对于较小的项目Euler 145,没有得到预期的结果

2024-10-16 22:31:27 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我正在研究Project Euler 145,上面写着:

Some positive integers n have the property that the sum [ n + reverse(n) ] consists entirely of odd (decimal) digits. For instance, 36 + 63 = 99 and 409 + 904 = 1313. We will call such numbers reversible; so 36, 63, 409, and 904 are reversible. Leading zeroes are not allowed in either n or reverse(n).

There are 120 reversible numbers below one-thousand.

How many reversible numbers are there below one-billion (10**9)?

我正在尝试以下代码(而不是使用10^9,而是使用10来检查结果(应该是零)是否正在发生:

def check(x):
    y = int(str(x)[::-1]) #x backwards
    #add the rev number to the original number (convert them to a list)
    xy = list(str(x+y))
    #a list of even digits.
    evens = ['0', '2', '4', '6', '8']
    #check if the number has any digits using intersection method.
    intersect = set(xy).intersection(set(evens))
    if not intersect:
        #if there was no intersection the digits must be all odd.
        return True
    return False

def firstCheck(x):
    if (int(str(x)[:1])+(x%10))%2 == 0:
        #See if first number and last number of x make an even number.
        return False
    return True

def solve():
    L = range(1, 10) #Make a list of 10
    for x in L:
        if firstCheck(x) == False:
            #This quickly gets rid of some elements, not all, but some.
            L.remove(x)
    for x in L:
        if check(x) == False:
            #This should get rid of all the elements.
            L.remove(x)
    #what is remaining should be the number of "reversible" numbers.
    #So return the length of the list.
    return len(L)

print solve()

它分为两个部分:在solve方法中有一个firstCheck和{},第一个检查是快速消除一些数字(因此,当我制作一个10^9大小的列表时,我可以释放一些RAM)。第二个检查是去掉所有被认为不是“可逆数字”的数字。在第一个检查中,我只看第一个和最后一个数字是否为偶数,然后消除这个数字。在检查方法中,我把数字颠倒,把两个数字加在一起,形成一个列表,然后检查它是否与一个偶数列表相交,如果它确实从列表中消除了它。结果列表应该是“可逆”数字的元素数量,所以我取列表并返回其长度。{cd4{I>作为所需的结果。它没有消除的数字[4,8],我似乎不知道为什么。在


Tags: andofthefalsenumber列表returnif
2条回答

我看到两个问题。在

首先,您(两次)修改要迭代的列表:

for x in L:
    if firstCheck(x) == False:
        #This quickly gets rid of some elements, not all, but some.
        L.remove(x)

这将导致意想不到的和难以预测的行为。迭代列表的副本或使用列表理解进行简单筛选:

^{pr2}$

等等

第二,您没有检查以消除任何前导零,所以check(10)在应该为False时为True。修复这些问题后,您的代码似乎可以正常工作:

>>> len(solve(10))
0
>>> len(solve(1000))
120

[我刚刚添加了一个参数来选择范围。]

Java解决方案,需要3分钟

public class Euler145 {

public static void main(String[] args) {

  int i, j, add, convert, count = 0;
  String get;
  StringBuilder rev;

 for ( i=0; i <= 1000000000; i++) {
        get = "" + i;
        rev = new StringBuilder(get);
        rev.reverse():
        convert = Integer.parseInt(rev.toString());
        add = convert + i;

        if (add % 2 != 0)  {

            while (add > 0)  {
                 j = add % 10;

                 if (j == 1 || j == 3 || j == 5 || j == 7 || j == 9)  {
                    add /= 10;
                    String leadZero = "" + i;
                    if (j == 0 && !leadZero.endsWith("0"))  {
                        count++;
                    }
                   }  else  {
                     break;
                 }
            }
        }
   }
      System.out.println(count);
 }
}

相关问题 更多 >