fork download
  1. import math
  2.  
  3. def solve(n):
  4. # Calculate integer square root of n and avoid recalculating values
  5. sqrt_n = int(math.isqrt(n))
  6. return min(n - sqrt_n * sqrt_n, (sqrt_n + 1) * (sqrt_n + 1) - n)
  7.  
  8. if __name__ == '__main__':
  9. n = int(input().strip())
  10. print(solve(n))
  11.  
Success #stdin #stdout 0.02s 25584KB
stdin
Standard input is empty
stdout
import math

def solve(n):
    # Calculate integer square root of n and avoid recalculating values
    sqrt_n = int(math.isqrt(n))
    return min(n - sqrt_n * sqrt_n, (sqrt_n + 1) * (sqrt_n + 1) - n)

if __name__ == '__main__':
    n = int(input().strip())
    print(solve(n))