I want to get the multiplication of array elements using recursion.
arr is array, and n is length of the array.
if arr=[1, 2, 3], n=3, answer is 6.
I tried this, but error occurred.
def multiply(arr, n):
if n == 0:
return arr
else:
return arr[n] * \
multyply(arr[n - 1])
please help me.
>Solution :
You should implement it like this
def mul(arr):
if not arr:
return 1
return arr[0] * mul(arr[1:])