Project Euler #80: Square root digital expansion,

Project Euler #80: Square root digital expansion,

 1 from decimal import getcontext, Decimal
 2 
 3 
 4 def main():
 5     n = int(raw_input())
 6     p = int(raw_input())
 7 
 8     getcontext().prec = p+10 # 扩大精度,保证接过
 9     sum = 0
10 
11     for i in range(1,n+1):
12         nTemp = Decimal(i).sqrt()
13         if nTemp._isinteger() : # 自生函数的判断
14             sum += 0
15         else:
16             nTemp = str(nTemp)
17             for _ in range(p+1): # 空下来一个.,所以+1
18                 if nTemp[_] != '.':
19                     sum += int(nTemp[_])
20 
21     print sum
22 
23 main()

学习

  抽象化领会标题

  边界值难题

错误

  读题不细心

 

 

Link:

  

Euler #80: Square root digital
expansion, 1 from decimal import getcontext, Decimal 2 3 4 def main():
5 n = int(raw_input()) 6 p = int(raw_input()) 7 8
getcontext().prec…

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图