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

  • Thread starter Tony11235
  • Start date
  • Tags
    Program
In summary, the given program is designed to find the Greatest Common Divisor (gcd) of two whole numbers, A and B. The program uses a loop to continuously subtract the smaller number from the larger number until they become equal, which is the gcd. To prove the correctness of the program, induction can be used on the loop cycles to show that the gcd of x and y always remains equal to the gcd of A and B, and that the values of x and y decrease monotonically. Additional techniques for proving correctness may include structural induction and mathematical induction. It is also important to show that the program will terminate.
  • #1
Tony11235
255
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
  • #2
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...?
 
  • #3
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.
 
  • #4
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
 
  • #5
So basically show the loop invariant, gcd(x,y) = gcd(A,B), is always true?
 
  • #6
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?
 
  • #7
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.
 
  • #8
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?
 

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

1. What is the purpose of proving program {x=A, y=B} GCD(A,B) correct?

The purpose of proving program {x=A, y=B} GCD(A,B) correct is to ensure that the program is performing its intended function accurately and without errors. This is important in order to have confidence in the program's results and to prevent potential issues or bugs.

2. How is GCD(A,B) calculated in program {x=A, y=B}?

GCD(A,B) stands for Greatest Common Divisor and is calculated using the Euclidean algorithm in program {x=A, y=B}. This algorithm involves dividing the larger number by the smaller number and then using the remainder as the new divisor in the next iteration until the remainder is equal to 0. The last non-zero remainder is the GCD of A and B.

3. What are some common errors that can occur in program {x=A, y=B} GCD(A,B)?

Some common errors that can occur in program {x=A, y=B} GCD(A,B) include infinite loops, incorrect variable assignments, and incorrect implementation of the Euclidean algorithm. These errors can lead to incorrect results or the program crashing.

4. How can we prove that program {x=A, y=B} GCD(A,B) is correct?

There are several ways to prove that program {x=A, y=B} GCD(A,B) is correct. One way is to use mathematical induction, where we prove that the program produces the correct result for a base case (e.g. GCD(0,0) = 0) and then show that if the program works for any given inputs, it will also work for the next input. Another way is to use loop invariants, where we prove that the program maintains the desired property (e.g. GCD(A,B) remains the same after each iteration) throughout its execution.

5. Why is it important to prove the correctness of program {x=A, y=B} GCD(A,B)?

Proving the correctness of program {x=A, y=B} GCD(A,B) is important because it ensures that the program is producing accurate results and can be trusted to perform its intended function. This is especially crucial for programs that involve critical calculations or are used in important systems, as incorrect results can have serious consequences.

Similar threads

Back
Top