为了账号安全,请及时绑定邮箱和手机立即绑定

Python:Project Euler 145

Python:Project Euler 145

森林海 2021-03-22 21:18:49
我今天的问题是,我是否沿着正确的欧拉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);
查看完整描述

3 回答

?
子衿沉夜

TA贡献1828条经验 获得超3个赞

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


查看完整回答
反对 回复 2021-03-23
  • 3 回答
  • 0 关注
  • 196 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号