def truncate(s, eps):
    """
    Find the smallest k such that sum(s[:k]**2) \geq 1-eps.
    """
    mysum = 0.0
    k=-1
    while (mysum < 1-eps):
        k += 1
        mysum += s[k]**2
    return k+1