Here is a recursive algorithm in pseudocode that calculates the sum of the series `1 - 1/2 + 1/3 - 1/4 + ..... + 1/n`:
function sumSeries(n: integer, i: integer = 1): float
if i > n then
return 0
else if i is odd then
return 1/i + sumSeries(n, i+1)
else
return -1/i + sumSeries(n, i+1)
end if
end function
This algorithm takes as input the value of `n`, which represents the number of terms in the series. The function also takes an optional parameter `i` which represents the current term being calculated. The function calculates the value of the current term and adds it to the sum of the remaining terms by calling itself recursively with an incremented value of `i`. The base case is when `i` is greater than `n`, in which case the function returns 0.
Jawaban:
Here is a recursive algorithm in pseudocode that calculates the sum of the series `1 - 1/2 + 1/3 - 1/4 + ..... + 1/n`:
function sumSeries(n: integer, i: integer = 1): float
if i > n then
return 0
else if i is odd then
return 1/i + sumSeries(n, i+1)
else
return -1/i + sumSeries(n, i+1)
end if
end function
This algorithm takes as input the value of `n`, which represents the number of terms in the series. The function also takes an optional parameter `i` which represents the current term being calculated. The function calculates the value of the current term and adds it to the sum of the remaining terms by calling itself recursively with an incremented value of `i`. The base case is when `i` is greater than `n`, in which case the function returns 0.