Project Euler #80: Square root digital expansion

  • + 0 comments

    Python 3 solution:

    import decimal
    n=int(input().strip())
    p=int(input().strip())
    decimal.getcontext().prec=p+4
    total=0
    for i in range(2,n+1):
        if int(i**0.5)!=i**0.5:
            digits=list(str(decimal.Decimal(i).sqrt()))[:p+1]
            digits.remove('.')
            #print(len(digits))
            total+=sum(map(int,digits))
    print(total)