import sympy
import math


def squarest_grid_size(num_images):
    """Calculates the size of the most square grid for num_images.
    Calculates the largest integer divisor of num_images less than or equal to
    sqrt(num_images) and returns that as the width. The height is
    num_images / width.

    Args:
      num_images: he total number of images.

    Returns:
      A tuple of (height, width) for the image grid.

    """
    divisors = sympy.divisors(num_images)
    square_root = math.sqrt(num_images)
    width = 1
    for d in divisors:
        if d > square_root:
            break
        width = d
    return (num_images // width, width)