Problem 434610 · easy · Level 04 Non-Linear Data Structures

Largest Square Tile for a Floor

recursion · gcd · modulo

A tiler wants to cover a rectangular floor of size w by h (whole centimetres) with identical square tiles and no cutting. Write tile_size(w, h) returning the side length of the largest square tile that fits exactly, using a recursive definition (no math.gcd, no loops). If one side is 0, return the other side (so 0 when both are 0).

Examples

Input:  w = 84, h = 30
Output: 6
Explanation: 6 divides both 84 and 30 and nothing bigger does.

Input:  w = 7, h = 0
Output: 7

Constraints

  • 0 <= w, h <= 10**15
  • Recursion depth is at most ~75 for these sizes.

Goals

  • Reduce a pair of numbers with the remainder operation
  • Recognise that a zero second argument ends the recursion
  • Return the first argument at the base case
Starting Python…