Question

In: Computer Science

Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...

Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.

Solutions

Expert Solution

Program code-

def sum_odds(n):
if n==1:
return 1
else:
return (2*n-1)+sum_odds(n-1)
  
print(sum_odds(8))

Explanation :

1. We define a function named sum_odds which accepts postive integer n.

2.if n is 1 , then we return 1 since first positive odd integer is 1.

3.if n is not 1 , say n is 8 . Then we return (2*n -1) which 2*8-1=15 and keep calling sum_odds(n-1) recursively

when n =8,

2*n-1=2*8-1=15 and call function sum_odds(n-1) which is sum_odds(7)

when  n =7

15 +  (2*n-1=2*7-1=13) and call function sum_odds(n-1) which is sum_odds(6)

when n =6

15 + 13+ (2*n-1=2*6-1=11) and call function sum_odds(n-1) which is sum_odds(5)

when n=5

15 + 13 + 11 + (2*n-1=2*5-1=9) and call function sum_odds(n-1) which is sum_odds(4)

when n=4

15 + 13 + 11 + 9 + (2*n-1=2*4-1=7) and call function sum_odds(n-1) which is sum_odds(3)

when n=3

15 + 13 + 11 + 9 + 7 + (2*n-1=2*3-1=5) and call function sum_odds(n-1) which is sum_odds(2)

when n=2

15 + 13 + 11 + 9 + 7 + 5 + (2*n-1=2*2-1=3) and call function sum_odds(n-1) which is sum_odds(1)

when n=1 , we return 1

Hence 15+13+11+9+7+5+3+1 = 64 is finally computed by the function and returned .


Related Solutions

PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns the number of 1's in the binary representation of n. Use the fact that this is equal to the number of 1's in the representation of n//2 (integer division) plus 1 if n is odd. >>>numOnes(0) 0 >>>numOnes(1) 1 >>>numOnes(14) 3
IN PYTHON Write a program that takes in a positive integer as input, and outputs a...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string....
(Python) Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
In Python8.21 Program: Convert to binary. Sections 2.7, 3.8, 5.2. Functions.Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x // 2Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string.Your program must define and call the following two functions. The function integer_to_reverse_binary() should return...
Write a Python program which takes a set of positive numbers from the input and returns...
Write a Python program which takes a set of positive numbers from the input and returns the sum of the prime numbers in the given set. The sequence will be ended with a negative number.
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
USE Coral Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is 6, the output is:011(6 in binary is 110; the algorithm outputs the bits in reverse).
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
In Java  Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is:6the output is:0116 in binary is 110; the algorithm outputs the bits in reverse.
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer value n and returns an approximation of e as (1 + 1/n)^n I am not even sure what the question is asking me to do but I think it is asking me to code that function. Does anyone know how to do this?
[C] Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
6.19 LAB: Convert to binary - functionsWrite a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string.Ex: If the input is:6the output is:110Your program must define and call the following two functions. The IntegerToReverseBinary function...
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT