The aim of the algorithm is to find the sum of the reciprocal of Sequence A with n numbers, using recursive algorithm.
findSum(A,n){
Sum = 0
if (n == 1) {
return 1/A[0]
}
else {
return A[n-1]
Sum += 1/A[n-1]
}
return Sum
}
Can someone help me? Thanks a lot!!!
>Solution :
As other pointed out, you’re never calling the function within itself, so no, it’s not recursive.
There are multiple ways to go about this but you should think about what and where exactly are you summing up. You declared the sum within the function but you only take the reciprocal of the last item.
Think how would you go about the next iteration for the number before the last one. You can scope the sum outside the function, pass it as a parameter, or make the sum right in the return statement.
findSum(A, n){
return 1/A[n-1] + findSum(A, n-1)
}