Proving Program {x=A, y=B} GCD(A,B) Correct

  • Thread starter Thread starter Tony11235
  • Start date Start date
  • Tags Tags
    Program
Click For Summary

Discussion Overview

The discussion revolves around proving the correctness of a program that computes the Greatest Common Divisor (GCD) of two whole numbers A and B using a specific algorithm. Participants explore various proof techniques applicable to this algorithm, including induction methods.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant asks how to prove the correctness of the program that computes gcd(A, B) and seeks the best method for such a proof.
  • Another participant requests an example proof and inquires about the techniques taught for proving program correctness, including assumptions about the compiler.
  • Some participants mention using various forms of induction, questioning whether structural or mathematical induction can be applied to this program.
  • One suggestion is to use induction on the loop cycles to demonstrate that gcd(x, y) remains equal to gcd(A, B) and that the sum x + y is strictly monotone decreasing.
  • Participants discuss the importance of establishing a loop invariant, specifically that gcd(x, y) = gcd(A, B), and the need to show program termination.
  • Concerns are raised about the application of induction on loops, with one participant expressing uncertainty about how to proceed with this method.

Areas of Agreement / Disagreement

Participants generally agree on the need to establish a loop invariant and demonstrate termination, but there is no consensus on the specific methods of induction to use or how to apply them effectively.

Contextual Notes

Participants express uncertainty regarding the application of induction techniques to loops and the specific assumptions that can be made about the program's execution environment.

Who May Find This Useful

This discussion may be useful for students or practitioners interested in program correctness, proof techniques in computer science, and mathematical induction methods.

Tony11235
Messages
254
Reaction score
0
{x = A, y = B}
begin
while x != y do
if x < y
then y := y - x
else x := x - y
end
{x = gcd(A, B)}

How do I prove that the program above is correct? A and B are whole numbers and gcd(A, B) stands for Greatest Commom Divisor. What's the best method for proving that it computes the gcd of A and B? It's not written in any specific language.
 
Physics news on Phys.org
Can you show an example proof from your class? What techniques are you taught to use for a proof like this? What assumptions can you make about the compiler, etc...?
 
Methods that we have done include just various forms of induction. Can structural induction be used here? Mathematical induction? If so, I am not sure where to start when it comes to a program like this.
 
I would suggest induction on the loop cycles to show that:
1. gcd(x,y) is always equal to gcd(A,B)
and
2. x+y is strictly monotone decreasing
 
So basically show the loop invariant, gcd(x,y) = gcd(A,B), is always true?
 
NateTG said:
I would suggest induction on the loop cycles to show that:
1. gcd(x,y) is always equal to gcd(A,B)
and
2. x+y is strictly monotone decreasing

How do I perform induction on the loop cycles?
 
Tony11235 said:
So basically show the loop invariant, gcd(x,y) = gcd(A,B), is always true?

Right, that will show that if you get a result, the result will be gcd(x,y).
The other thing you need to show is that the program will terminate.
 
I'm really only familiar with mathematical induction. I really do not see how to perform induction on the loops to show that the program is correct. Any examples?
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 0 ·
Replies
0
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K