1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Alekssandra [29.7K]
4 years ago
14

Recursively computing the sum of the first n positive odd integers, cont. About (a) Use induction to prove that your algorithm t

o compute the sum of the first n positive odd integers returns the correct value for every positive integer input.
Computers and Technology
1 answer:
julia-pushkina [17]4 years ago
6 0

The recursive function would work like this: the n-th odd number is 2n-1. With each iteration, we return the sum of 2n-1 and the sum of the first n-1 odd numbers. The break case is when we have the sum of the first odd number, which is 1, and we return 1.

int recursiveOddSum(int n) {

 if(2n-1==1) return 1;

 return (2n-1) + recursiveOddSum(n-1);

}

To prove the correctness of this algorithm by induction, we start from the base case as usual:

f(1)=1

by definition of the break case, and 1 is indeed the sum of the first odd number (it is a degenerate sum of only one term).

Now we can assume that f(n-1) returns indeed the sum of the first n-1 odd numbers, and we have to proof that f(n) returns the sum of the first n odd numbers. By the recursive logic, we have

f(n)=f(n-1)+2n-1

and by induction, f(n-1) is the sum of the first n-1 odd numbers, and 2n-1 is the n-th odd number. So, f(n) is the sum of the first n odd numbers, as required:

f(n)=\underbrace{\underbrace{f(n-1)}_{\text{sum of the first n-1 odds}}+\underbrace{2n-1}_{\text{n-th odd}}}_{\text{sum of the first n odds.}}

You might be interested in
Question 1<br> What does the % find?<br> The product<br> The mode<br> The quotient<br> The remainder
Paul [167]
The correct answer is the remainder
7 0
4 years ago
What are some useful applications of video games?
vekshin1

Answer:

Steam, Origin, Uplay and epic games

Explanation:

8 0
3 years ago
All linear programming problems have all of the following properties EXCEPT
lyudmila [28]

Answer:

alternative optimal solutions

Explanation:

8 0
4 years ago
Read 2 more answers
Write is the use of find and replace cpmmand​
jeka94

Answer:

Control H is the command to Find and Replace. It is used to find a word and replace the word with something else.

Explanation:

5 0
3 years ago
Enter key is also known as Return key. (True or false)
Amanda [17]

Answer:

I think false

hope it helps

6 0
3 years ago
Read 2 more answers
Other questions:
  • A customer has a system with a Gigabyte B450 Aorus Pro motherboard. He wants to upgrade the processor from the AMD Athlon X4 950
    8·2 answers
  • What is NOT powered by the PSU of your computer?
    9·1 answer
  • You will write a program that generates random drawings that can be drawn using the shape painter.
    5·1 answer
  • A __________ is a combination of software and hardware that links two different types of networks.
    14·1 answer
  • QUESTION 2 Fruit is an enumerated type with values apple, banana, grape, and orange, in that order. Which of the following state
    8·1 answer
  • Help me please!! It would be appreciated :)
    8·2 answers
  • Does anyone want to play nitro type with me for my typing class I put 100 points into this so If you put link or random stuff I
    7·1 answer
  • Write a program to generate the following series 1, 3, 5, 7, 9, 11.................. 10th​
    10·1 answer
  • How to make classs constructer java.
    14·1 answer
  • _________ in online education refer(s) to how fairly the particular needs of particular groups of students are met.
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!