Page 1 of 1

1) We are trying to write a recursive algorithm that takes a positive integer input n so that: If n is odd, compute the

Posted: Mon Mar 21, 2022 4:48 pm
by answerhappygod
1) We are trying to write a recursive algorithm that takes a
positive integer input n so that:
If n is odd, compute the sum of all odd integers from 1 to
n
if n is even, compute sum of all even integers from 1 to
n
Would the function shown below work? If not, provide
corrections.
def sum_alternate(n):
if n == 0:
return 0
return n + sum_alternate( n-2 )
And what is the returned value of the following code?
sum_alternate(7)