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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

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

Post 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)
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply