def METHOD_NAME(n, K):
    """Low-level implementation of Legendre polynomials."""
    ring = K.poly_ring('_0')
    x = ring._0
    l0 = ring.one
    if n < 1:
        return l0
    l1 = x
    for i in range(2, n + 1):
        l0, l1 = l1, l1*x*K(2*i - 1, i) - l0*K(i - 1, i)
    return l1