Skip to content

Commit ce16bfa

Browse files
Merge pull request souravjain540#73 from misbahulhaque234/main
Added Recursion Questions
2 parents 925af7a + e446143 commit ce16bfa

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

GeoMetric_sum.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Geometric Sum = 1 + 1/2 + 1/4 + 1/8 + 1/16 + ... + 1/(2^n)
2+
# where n is the input from the user
3+
# Example:
4+
# Input: 3
5+
# Output: 1.875
6+
7+
def geometricSum(n):
8+
if n == 0: # base case
9+
return 1 # return 1 if n == 0
10+
smallOutput = geometricSum(n - 1) # recursion call
11+
# adding and smallOutput vaule + 1 and divide using 2^n
12+
return smallOutput + 1 / pow(2, n)
13+
14+
15+
n = int(input()) # taking input from user
16+
print(geometricSum(n)) # calling the function and printing the result

0 commit comments

Comments
 (0)