Project Euler:Problem 80 Square root digital expansion

It is well known that if the square root of a natural number is not an integer, then it is irrational. The decimal expansion of such square roots is infinite without any repeating pattern at all.

The square root of two is 1.41421356237309504880..., and the digital sum of the first one hundred decimal digits is 475.

For the first one hundred natural numbers, find the total of the digital sums of the first one hundred decimal digits for all the irrational square roots.



难道是我理解错了!!the first one hundred decimal digits 这个意思不是说前一百位小数的数字??!!小数点前面的要算么!!总之最后是算上小数点前面的数提交才通过的(╯‵□′)╯︵┻━┻


具体方法参照的这里 这题感觉不只知道这个方法的话解决起来会有点麻烦


import math

def count(n):
    limit=math.pow(10,102)
    a=n*5
    b=5
    while b<limit:
        if a>=b:
            a=a-b
            b=b+10
        else:
            a=a*100
            if b!= 5:
                b=(b-5)*10+5
    b_str=str(b)
    return b_str[:-3]


def digitalcount(s):
    ans=0
    c=0
    for i in range(0,len(s)):
        tmp=int(s[i])-int('0')
        ans=ans+tmp
        c=c+1
    print(c)
    return ans



ans = 0
j=1
for i in range(1,101):
    if j*j==i:
        j=j+1
        continue
    a=digitalcount(count(i))
    ans=ans+a

print('ans = ',ans)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值