What is the loop invariant for proving the LCM algorithm?

  • Thread starter flying2000
  • Start date
  • Tags
    Algorithm
In summary, the conversation discusses a possible algorithm to prove the least common multiple (LCM) of two numbers, m and n. The conversation mentions different loop invariants that could potentially prove the LCM, but it is ultimately concluded that these invariants may not be sufficient. The algorithm is solved when considering additional conditions such as the preconditions of m and n being greater than or equal to 1.
  • #1
flying2000
40
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
  • #2
Forget it! problem soloved ...

Forget it! problem soloved ...
 
  • #3
I'm curious, how do you solve that?
 
  • #4
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..
 
  • #5
Cool, I see.
 
  • #6
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:
  • #7
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..
 
  • #8
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.
 

1. What is an LCM?

The LCM (least common multiple) is the smallest positive integer that is divisible by two or more numbers without a remainder. It is commonly used in mathematics to find the smallest common denominator or to solve problems involving fractions.

2. How do you find the LCM of two numbers?

To find the LCM of two numbers, you can use a variety of methods such as prime factorization, listing multiples, or using the "cake method". The most efficient method is to use prime factorization by breaking down each number into its prime factors and then multiplying the highest power of each prime factor together to find the LCM.

3. What is an algorithm for finding the LCM?

An algorithm is a set of steps or rules used to solve a problem or complete a task. The algorithm for finding the LCM involves breaking down the numbers into their prime factors and then multiplying the highest power of each prime factor together to find the LCM.

4. Why is proving the algorithm for LCM important?

Proving the algorithm for LCM is important because it helps to ensure that the method used to find the LCM is accurate and reliable. It also allows for a better understanding of how the algorithm works and how it can be applied to other mathematical concepts.

5. Can the LCM algorithm be used for more than two numbers?

Yes, the LCM algorithm can be used for any number of integers. Simply follow the same steps of breaking down each number into its prime factors and then multiplying the highest power of each prime factor together to find the LCM of all the numbers.

Similar threads

  • Set Theory, Logic, Probability, Statistics
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
961
  • Linear and Abstract Algebra
Replies
3
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
1K
Replies
1
Views
813
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
1K
Replies
9
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Calculus and Beyond Homework Help
Replies
1
Views
2K
Back
Top