def sum_gp(a, n, r):
    total = 0
    term = a
    for _ in range(n):
        total += term
        term *= r
    return total