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:

  https://www.hackerrank.com/contests/projecteuler/challenges/euler080

你可能感兴趣的:(project)