Problem 546081 · medium · Phase 05 Advanced Algorithms & Graphs

Biggest Square Garden From the Tiles

number theory · Newton's method · integer arithmetic

You have tiles unit tiles and want to lay the biggest possible square garden. Return a tuple (side, leftover): the side length of the largest square you can fill and how many tiles remain. tiles can be up to 60 digits long, so floating-point sqrt is not accurate enough. Use integer arithmetic only (no math.isqrt, no ** 0.5).

Examples

Input:  tiles = 50
Output: (7, 1)
Explanation: 7 * 7 = 49 uses 49 tiles, 1 is left.
Input:  tiles = 0
Output: (0, 0)

Constraints

  • 0 <= tiles <= 10**60
  • Target complexity: O(log tiles) iterations of big-integer arithmetic.

Goals

  • Compute a floor square root using only integers
  • Apply Newton's iteration x -> (x + n // x) // 2 and know when to stop
  • Stay exact for numbers far beyond float precision
Starting Python…