import math


def parents(level, idx):
    """
    Return all the (grand-)parents of the Healpix pixel idx at level (in nested format)

    :param level: Resolution level
    :param idx: Pixel index
    :return: All the parents of the pixel
    """
    assert idx < 12 * 2 ** (2 * level)
    plpairs = []
    for ind in range(level, 0, -1):
        idx = int(math.floor(idx / 4))
        plpairs.append(tuple((ind - 1, idx)))
        level -= 1
    return plpairs[::-1]