fork download
  1. from math import log10
  2.  
  3. def long_sqrt(number, precision=0):
  4. # Split number into digit pairs
  5. pairs = [int(number // 10 ** x % 100) for x in range(0,
  6. int(log10(number) + 1), 2)[::-1]]
  7. if precision > 0:
  8. pairs.extend([int(number * 10 ** (2 * x) % 100) for x in
  9. range(1, precision + 1)])
  10. # Get the first digit: Solve A^2 <= [first pair]. This is the initial result.
  11. start = pairs.pop(0)
  12. result = max(filter(lambda x: x ** 2 <= start, range(10)))
  13. remainder = start - result ** 2
  14. while pairs:
  15. # Bring down two digits
  16. value = 100 * remainder + pairs.pop(0)
  17. # Calculate B. Start with its highest possible value, then decrement as necessary
  18. next_digit = value // 20 // result
  19. while (result * 20 + next_digit) * next_digit > value:
  20. next_digit -= 1
  21. # Plug in B and calculate new remainder
  22. remainder = value - ((result * 20 + next_digit) * next_digit)
  23. # Add B to the final result
  24. result = 10 * result + next_digit
  25. # If precision was specified, we need to divide to put the decimal point in place. Otherwise, we have our int.
  26. return result / 10 ** precision if precision else result
  27.  
  28. # Check sample inputs
  29. assert long_sqrt(7720.17, 0) == 87
  30. assert long_sqrt(7720.17, 1) == 87.8
  31. assert long_sqrt(7720.17, 2) == 87.86
  32. # Run challenge inputs
  33. print(long_sqrt(12345))
  34. print(long_sqrt(123456, 8))
  35. print(long_sqrt(12345678901234567890123456789, 1))
Success #stdin #stdout 0.02s 28384KB
stdin
Standard input is empty
stdout
111
351.36306009
111111110611111.1