我今天的問題是,如果我在歐拉145的正確道路上走下去,如果它是一種有點高效。我的大部分時間都沒有完成,只是我的一個Defs給了我一個int(str(numb)[:i])%2 == 0的麻煩來進行偶校驗。我的代碼在下面。第10行是問題點Python:Project Euler 145
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);
PLE ase從不在Python中使用';'s。除此之外,看'str(1234)[:0]'是 – 2013-05-11 03:46:08
另外,'revers'是一個內置函數 - 你不應該覆蓋它。 – Volatility 2013-05-11 03:53:09