Subtract S from Sr
Sr - S = ar^2 + ar^3 + ar^4 + ... + ar^p + ar^(p + 1) - (ar + ar^2 + ar^3 + ... + ar^p)
If you distribute the negative sign correctly, you'll see that most of the terms on the right hand side of the equation cancel out:
Sr - S = ar^(p + 1) - ar
Sr - S = ar * r^p - ar
S * (r - 1) = ar * (r^p - 1)
Solve for S
S = ar * (r^p - 1) / (r - 1)
To test, let's look at the series I listed before. Let's add the first 5 terms together:
1 + 2 + 4 + 8 + 16 => 31
That's easy enough to do in our heads, but let's use the formula and solve the sum
a = 1/2
r = 2
p = 5
(1/2) * 2 * (2^5 - 1) / (2 - 1) = 1 * (32 - 1) / 1 = 31
What if we wanted to add the first 100 terms? Can you see how this formula will save us a lot of headaches at that point? And this formula applies to all geometric series
S = ar * (r^p - 1) / (r - 1)