Skip to content

Commit d5e4209

Browse files
committed
Sum of Geometric Progression
1 parent 5a3353a commit d5e4209

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Data Structures and Algorithms implementation in Python.
44

55
### Topics covered:
66
- [x] [Recursion](Recursion)
7-
- [x] [Basic-Problems](Recursion/Basic-Problems)
7+
- [x] [Basic Problems](Recursion/Basic-Problems)
88
- [x] [Factorial](Recursion/Basic-Problems/factorial.py)
9-
- [x] [Fibonacci](Recursion/Basic-Problems/fibonacci.py)
9+
- [x] [Fibonacci](Recursion/Basic-Problems/fibonacci.py)
10+
- [x] [Sum of Geometric Progression](Recursion/Basic-Problems/GP.py)

Recursion/Basic-Problems/GP.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'''
2+
Geometric Progression
3+
The nth term of a GP series is Tn = arn-1, where a = first term and r = common ratio = Tn/Tn-1) . The sum of infinite terms of a GP series S∞= a/(1-r) where 0< r<1. If a is the first term, r is the common ratio of a finite G.P.
4+
5+
6+
Given an integer N, we need to find the geometric sum of the following series using recursion.
7+
8+
1 + 1/3 + 1/9 + 1/27 + … + 1/(3^n)
9+
10+
Input N = 5
11+
Output: 1.49794
12+
13+
Input: N = 7
14+
Output: 1.49977
15+
16+
Approach:
17+
We will calculate the last term and call recursion on the remaining n-1 terms each time. The final sum returned is the result.
18+
'''
19+
20+
def geo_sum(n):
21+
if n==0:
22+
return 1
23+
24+
sum = 1/pow(2,n) + geo_sum(n-1)
25+
return sum
26+
27+
print(geo_sum(8)) #1.499923792104862

0 commit comments

Comments
 (0)