Teaching Kids Programming: Videos on Data Structures and Algorithms
Geometric Progression of numbers are:
where q is neither 1 or 0. We want to compute the sum, aka
Sum of Geometric Progression (Math Proof)
Let where , and
Multiple both sides with q (since q can’t be zero)
which is
We can subtract both side from aka
Therefore
Python Algorithm to Compute the Sum of Geometric Progression
Here is the simple python function to compute the sum of a geometric progression.
1 2 3 4 5 | def sum(a, q, n): s = 0 for i in range(n): s += a * (q ** i) return s |
def sum(a, q, n): s = 0 for i in range(n): s += a * (q ** i) return s
We can also use math.pow(q, i) to compute the value of q to the power of i.
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading...
736 wordsloading...
Last Post: Teaching Kids Programming - Delete the Middle Node of a Linked List (Fast and Slow Pointer)
Next Post: Teaching Kids Programming - Words That Can Be Typed using a Single Keyboard Row (Hash Set)