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: The total number of images.

    Returns:
      A tuple of (height, width) for the image grid.
    """
    square_root = math.sqrt(num_images)
    if square_root.is_integer():
        width = int(square_root)
    else:
        width = math.floor(square_root)
        while not num_images % width == 0:
            width -= 1

    return num_images // width, width