Question: Consider the following code which determines whether x and y are relatively prime (that is, there are no integers 2 that divide evenly into
Consider the following code which determines whether x and y are relatively prime (that is, there are no integers ≥ 2 that divide evenly into both x and y):
bool IsRelativelyPrime(int x, int y) {
for (int i = 2; i <= x && i <= y; i++) {
if ((x % i == 0) && (y % i == 0)) return false;
}
return true;
}
You may assume that x and y are both at least 2.
Identify the loop invariant for the above code, prove the correctness of your invariant using induction, and then use the loop invariant to prove the correctness of the algorithm.
Step by Step Solution
3.50 Rating (163 Votes )
There are 3 Steps involved in it
To address the question lets identify the loop invariant and prove its correctness using induction and then use this loop invariant to prove the corre... View full answer
Get step-by-step solutions from verified subject matter experts
