Finding the first term of a geometric series by the sum and $n$

I have the following exponential series:

$$S = ar^0 + ar^1 + ar^2 + \cdots + ar^n$$

I know $S$, $r$ and $n$. How do I find $a$?

I actually need this done by a script so all "crazy" methods like doing an operation $n$ times are ok.

-
This is a geometric series. – Raskolnikov Apr 14 '11 at 12:28
I changed the title. – Américo Tavares Apr 15 '11 at 11:55

As Raskolnikov points out this is a geometric series. Sum is given by $$S = a \cdot \frac{r^{n+1}-1}{r-1}$$
Substitute the value of $S,r,n$ to get $a$
actually by the link Raskolnikov gave its $S = a \cdot \frac{r^{n+1}-1}{r-1}$ because my series terminates in $n$ not $n-1$ – Dani Apr 14 '11 at 12:36
@Dani: Yes, Dani because here there are $n+1$ terms. The formula which I gave was for $n$ terms. – user9413 Apr 14 '11 at 12:38