Upload 1B.py
Browse files
1B.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def binary_sum(S, start, stop):
|
| 2 |
+
if start>=stop:
|
| 3 |
+
return 0
|
| 4 |
+
elif start == stop-1:
|
| 5 |
+
return S[start]
|
| 6 |
+
else:
|
| 7 |
+
mid= (start+stop)//2
|
| 8 |
+
return binary_sum(S, start, mid)+binary_sum(S, mid,stop)
|
| 9 |
+
print("\t summing the elements of a sequence using binary recursion")
|
| 10 |
+
print("\t -----------------------------------------")
|
| 11 |
+
S=[]
|
| 12 |
+
print("Input")
|
| 13 |
+
print("- - - - -")
|
| 14 |
+
n=int(input("enter the no of elements:"))
|
| 15 |
+
print("Enter the elements one by one:")
|
| 16 |
+
for i in range(n):
|
| 17 |
+
m=int(input())
|
| 18 |
+
S.append(m)
|
| 19 |
+
print("The given sequence of elements=",S)
|
| 20 |
+
r=binary_sum(S, 0, n)
|
| 21 |
+
print("output")
|
| 22 |
+
print("- - - - - -")
|
| 23 |
+
print("The given sum of sequence of elements=",r)
|