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)
1) We are trying to write a recursive algorithm that takes a positive integer input n so that: If n is odd, compute the
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am