A geometric progression is a series in which each successive term is generated by multiplying the term before it by a fixed value called the common ratio. Given that the first term is $a$ and the common ratio is $r$, the $nth$ term of a GP is: $$ \begin{equation}\begin{aligned} u_n=ar^{n-1}\\ \end{aligned}\end{equation} $$
The sum up to the $nth$ term is: $$ \begin{equation}\begin{aligned} S_n&=\frac{a(r^{n-1}-1)}{r-1}\\ \end{aligned}\end{equation} $$
For fractional and negative values of $r$, we use a variation of this formula: $$ \begin{equation}\begin{aligned} S_n&=\frac{a(1-r^{n-1})}{1-r}\\ \end{aligned}\end{equation} $$
The sum of all terms
We can find the sum of all terms in a GP by considering the limit of the sum formula as $n\rightarrow \infty$: $$ \begin{equation}\begin{aligned} S_{\infty}&=\lim_{n\rightarrow \infty} S_n\\ &=\lim_{n\rightarrow \infty} \frac{a(1-r^{n-1})}{1-r}\\ &=\frac{a}{1-r}\\ \end{aligned}\end{equation} $$
This value is achieved because as $n$ approaches infinity, the term $r^{n-1}$ becomes more insignificant (tends to zero, $0$). This is because a fraction ($r$ in this case) when raised to higher powers eventually becomes zero.
Code to generate a geometric progression
Here is the Golang code to generate the terms and sum of terms in a geometric progression:
package main
import (
"fmt"
"math"
)
func main() {
var a float64
var r float64
AcceptFirstTerm:
fmt.Println("Please enter the first term:")
_, err := fmt.Scanf("%g", &a)
if err != nil {
fmt.Println("Error reading 'a':", err)
goto AcceptFirstTerm
}
AcceptCommonRatio:
fmt.Println("Please enter the common ratio:")
_, err = fmt.Scanf("%g", &r)
if err != nil {
fmt.Println("Error reading 'r':", err)
goto AcceptCommonRatio
}
sum := float64(0)
for n := 1; n <= 10; n++ {
u_n := a * math.Pow(r, float64(n-1))
sum += u_n
fmt.Printf("u_%d = %g, S_%d = %g\n", n, u_n, n, sum)
}
}