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