import math


def APR(n,m,alpha=0.08,s=0.20):
    """
    Return number of (equal-sized) precincts to audit.
    Use Aslam/Popa/Rivest (nearly exact) approximation formula
       n = number of precincts
       m = margin of victory as a fraction of total votes cast
       alpha = significance level desired = 1 - confidence
       s = maximum within-precinct-miscount
    """
    if m == 0.0:
        return n
    b = math.ceil(n * (m / (2 * s)))
    u = (n - (b-1)/2.0)*(1.0-math.pow(alpha,1.0/b))
    return u