def _geom_series_uint32(r, n):
    """Unsigned integer calculation of sum of geometric series:
    1 + r + r^2 + r^3 + ... r^(n-1)
    summed to n terms.
    Calculated modulo 2**32.
    Use the formula (r**n - 1) / (r - 1)
    """
    if n == 0:
        return 0
    if n == 1 or r == 0:
        return 1
    m = 2**32
    # Split (r - 1) into common factors with the modulo 2**32 -- i.e. all
    # factors of 2; and other factors which are coprime with the modulo 2**32.
    other_factors = r - 1
    common_factor = 1
    while (other_factors % 2) == 0:
        other_factors //= 2
        common_factor *= 2
    other_factors_inverse = pow(other_factors, m - 1, m)
    numerator = pow(r, n, common_factor * m) - 1
    return (numerator // common_factor * other_factors_inverse) % m
Unsigned integer calculation of sum of geometric series:
    1 + r + r^2 + r^3 + ... r^(n-1)
    summed to n terms.
    Calculated modulo 2**32.
    Use the formula (r**n - 1) / (r - 1)