We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 925af7a + e446143 commit ce16bfaCopy full SHA for ce16bfa
GeoMetric_sum.py
@@ -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