Discrete StructureTU Board 2023
Prove that the following recursive algorithm to compute aⁿ, is correct using mathematical induction. int power(int a, int n) if (n == 0) return 1; else return a power (a, n 1);
5Answer
Proof by Mathematical Induction
To prove the correctness of the recursive algorithm for computing , we use mathematical induction.
1. Base Case (n = 0)
For , the algorithm returns:
if (n == 0) return 1;
By definition, . Thus, the base case holds:
2. Inductive Hypothesis (n = k)
Assume that for some integer , the algorithm correctly computes :
3. Inductive Step (n = k + 1)
We must show that if the hypothesis holds for , then it also holds for .
The recursive call for is:
power(a, k + 1) = a * power(a, k).
By the inductive hypothesis, . Substituting: Thus, the algorithm correctly computes .
Conclusion
By the principle of mathematical induction, the algorithm computes correctly for all non-negative integers .
// The algorithm works by:
// 1. Returning 1 when n = 0 (base case).
// 2. Recursively multiplying a by power(a, n-1) for n > 0.
Discussion
Loading…