We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
from decimal import Decimal,getcontext
def sum_of_digits(n,p):
getcontext().prec=p+5
sqrt_str=str(Decimal(n).sqrt()).replace('.','')[:p]
return sum(int(digit) for digit in sqrt_str)
def main():
N= int(input().strip())
P= int(input().strip())
total_sum= sum(sum_of_digits(i,P) for i in range (1,N+1) if int (i**0.5)**2 != i)
print(total_sum)
if name=="main":
main()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #80: Square root digital expansion
You are viewing a single comment's thread. Return to all comments →
My Solution in python
from decimal import Decimal,getcontext def sum_of_digits(n,p): getcontext().prec=p+5 sqrt_str=str(Decimal(n).sqrt()).replace('.','')[:p] return sum(int(digit) for digit in sqrt_str) def main(): N= int(input().strip()) P= int(input().strip()) total_sum= sum(sum_of_digits(i,P) for i in range (1,N+1) if int (i**0.5)**2 != i) print(total_sum) if name=="main": main()