-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathGP.py
27 lines (18 loc) · 767 Bytes
/
GP.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
'''
Geometric Progression
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.
Given an integer N, we need to find the geometric sum of the following series using recursion.
1 + 1/3 + 1/9 + 1/27 + … + 1/(3^n)
Input N = 5
Output: 1.49794
Input: N = 7
Output: 1.49977
Approach:
We will calculate the last term and call recursion on the remaining n-1 terms each time. The final sum returned is the result.
'''
def geo_sum(n):
if n==0:
return 1
sum = 1/pow(2,n) + geo_sum(n-1)
return sum
print(geo_sum(8)) #1.499923792104862