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...
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the...
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the total sum of the digits in the integers from 1 to that number inclusive. b. Write a program to input an integer n and call the above function in part a if n is positive, else give ‘Value must be Positive’ message. sample: Enter a positive integer: 1000000 The sum of the digits in the number from 1 to 1000000 is 27000001 Enter a...
Develop, write and certify a properly recursive procedure reverseDigits to input a positive integer n and...
Develop, write and certify a properly recursive procedure reverseDigits to input a positive integer n and to output the integer formed by reversing the digits of n. Thus (reverseDigits 1234) returns the integer 4321. Please answer using Dr Racket(R5RS language)
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 Python function next_Sophie_Germain(n), which on input a positive integer n return the smallest Sophie...
Write a Python function next_Sophie_Germain(n), which on input a positive integer n return the smallest Sophie Germain prime that is greater or equal to n.
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...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT