What is the loop invariant for proving the LCM algorithm?

  • Context: Graduate 
  • Thread starter Thread starter flying2000
  • Start date Start date
  • Tags Tags
    Algorithm
Click For Summary

Discussion Overview

The discussion revolves around identifying the loop invariant for an algorithm designed to compute the least common multiple (LCM) of two integers, m and n. Participants explore various proposed loop invariants and their implications for proving the correctness of the algorithm.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant suggests a loop invariant of the form lcm(ak, bk) = lcm(ak/m, bk/n) * lcm(m,n) but expresses uncertainty about its validity.
  • Another participant proposes three conditions as loop invariants: ak >= m and bk >= m, ak mod m = 0 and bk mod n = 0, and ak <= lcm(m,n) and bk <= lcm(m,n).
  • A later reply introduces the idea that ak > a(k-1) and bk > b(k-1) could be relevant but notes that this condition does not hold until the second iteration.
  • There is a challenge to the proposed invariants, with one participant questioning their applicability by providing a counterexample involving a different loop structure.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the correct loop invariant. Multiple competing views are presented, and the discussion remains unresolved regarding the validity and applicability of the proposed invariants.

Contextual Notes

Some participants mention preconditions such as m, n >= 1, but the implications of these conditions on the loop invariants are not fully explored.

flying2000
Messages
40
Reaction score
0
algorithm to prove lcm:

a:=m
b:=n
while a != b
if a < b
a:= a + m
else
b:= b + n

//postcondition: a is the lcm(m,n)

what's the loop invariant?I thought it is(not sure):
lcm(ak, bk) = lcm(ak/m, bk/n) *lcm(m,n)
I am not sure and also impossible to prove my loop invariant...
Another loop invariant I thought is "ak=qm and bk=rn",but I don't know how to
show it's the lcm of m and n,
Thanks in advance!
 
Physics news on Phys.org
Forget it! problem soloved ...

Forget it! problem soloved ...
 
I'm curious, how do you solve that?
 
loop invariant..

Bartholomew said:
I'm curious, how do you solve that?

the loop invariant should be
1)ak >=m and bk> =m
2)ak mod m=0 and bk mod n=0
3)ak<=lcm(m,n) and bk<=lcm(m,n)

U can prove a is the lcm(m,n) by these LI..
 
Cool, I see.
 
Wait, I think you also need ak > a(k-1) and bk > b(k-1)

That's not really a loop invariant though because it's not true until the second iteration.

I guess you could say something like "if k is > 1, then (that condition) is true."
 
Last edited:
U right..

Bartholomew said:
Wait, I think you also need ak > a(k-1) and bk > b(k-1)

That's not really a loop invariant though because it's not true until the second iteration.

I guess you could say something like "if k is > 1, then (that condition) is true."


I forget to mention that.. m,n>=1 is the precondition..
 
No, that's not what I said. For example, what if the loop were:
for(i = 0; i < 10; i++)
{
a = m;
b = n;
}

That satisfies the loop invariants you gave, but it's not going to find the LCM.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 11 ·
Replies
11
Views
5K
Replies
1
Views
3K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K