1 / 4

Proving Loop Invariants in Recursive Functions Using Mathematical Induction

This document analyzes a specific function, SQ(A), which loops until a condition based on a positive integer A is met. It outlines the process of mathematical induction to verify that the relationship P(n) holds true for all n ≥ 0, demonstrating that Cn = A × Dn remains valid. Through a stepwise approach, beginning with base case P(0) and assuming the predicate holds for n = k, we show it also holds for n = k + 1. This ensures that the function behaves correctly as expected, indicating termination of the loop under specified conditions.

ulfah
Download Presentation

Proving Loop Invariants in Recursive Functions Using Mathematical Induction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Example

  2. Function SQ(A) C=0 D=0 While (D  A) a. C=C+A b. D=D+1 Return (A) A must be a positive integer if the looping is to end. A few trials with particulars values of A will provide evidence that the function does carry out this task.

  3. Let P(n) be the predicate Cn=A X Dn. Our task is to prove that n  0 P(n) is true. Here n0 is 0. • Step 1: P(0) is the statement C0=A X D0, which is true since the value of both C and D is zero after zero passes through the WHILE loop. • Step 2: Assume P(k) is true : Ck =A X Dk ………(i)

  4. Step 3: To prove Ck+1=A X Dk+1 is true. After a pass through the loop, C is increased by A and D is increased by 1, so Ck+1=Ck + A and Dk+1= Dk +1. LHS P(k+1): Ck+1= Ck + A = A X Dk + A = A (Dk +1) = A Dk+1 By MI it follows that as long as looping occurs Cn=A X Dn. The loop must terminates.

More Related