Skip to content

Instantly share code, notes, and snippets.

@pysoftware
Last active March 17, 2019 05:54
Show Gist options
  • Save pysoftware/ce61473b8e52f44c95f7ce32fa882c01 to your computer and use it in GitHub Desktop.
Save pysoftware/ce61473b8e52f44c95f7ce32fa882c01 to your computer and use it in GitHub Desktop.
Recursive sum
'''N IS REQUIRED PARAMETЕR'''
def recursive_sum1(arr, n):
n -= 1
if n < 0:
return 0
return arr[n] + recursive_sum1(arr, n)
def recursive_sum2(arr, n):
# n - value of numbers
if n == len(arr):
return 0
return arr[n] + recursive_sum2(arr, n+1)
def recursive_sum3(a,b):
if a == 0:
return b
return recursive_sum3(a-1, b+1)
a = [1, 2, 3, 4, 5]
print(recursive_sum1(a, len(a)))
print(recursive_sum2(a, 0))
print(recursive_sum3(5,5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment