Thread: Finding LCM--Driving me nuts

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Finding LCM--Driving me nuts

    I am having the hardest time finding the lowest common denominator.
    I have it set up in a for loop, for the life of me I can't find whats wrong.
    Here is what I have so far:
    Code:
    for (x=0;x == LCM;++x)
    		{
    			var1 = x * denom1;  
    			var2 = x * denom2;             
                            cout <<var1 <<"        " <<var2 <<endl;
                            if (var2 == var1)
                            {
                             x = LCM;
                             LCM = var1;
                            cout <<"The LCM is " <<LCM <<endl;
                           }
    		}
    It doesn't even seem like the for loop is finishing. It doesn't even cout the cout statment, it just continues with the rest of the program.
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>It doesn't even seem like the for loop is finishing. It doesn't
    even cout the cout statment, it just continues with the rest of the
    program.

    It would appear that your logic is way out, because you didn't
    even state the observed behavior properly - how can the loop
    appear to never end AND continue with the program without
    writing to the console?

    My initial guess is that value of the LCM variable at the start of the
    program has some silly value, but even still, how is this supposed
    to be determining the LCM?

    Code:
    var1 = x * denom1;
    var2 = x * denom2;
    cout <<var1 <<"        " <<var2 <<endl;
    if (var2 == var1)
    {
    Naturally, x is constant over these few lines of code, so how
    exactly do you expect multiplying denom1 and denom2 by
    x to change the the comparison test between var1 and var2?
    basically those lines may as well simplify to:

    if (denom1 == denom2)

    To get really basic, look at this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main (void)
    {
    	int denom1 = 1;
    	int denom2 = 2;
    	int var1, var2, x;
    
    	for (x = 1; x <= 10; x++)
    	{
    		cout << "Denom1 = " << denom1 << " Denom2 = " << denom2 << endl;
    
    		var1 = denom1 * x;
    		var2 = denom2 * x;
    
    		cout << "Var1 = " << var1 << " Var2 = " << var2 << endl << endl;
    	}
    
    	cout << "So Denom1 is always less than Denom 2, accordingly Var1 is always less than" << endl;
    	cout <<"Var2" << endl;
    
    	return 0;
    }
    Here's what Wikipedia has on LCM, take a look at the greatest
    common divisor (GCD) too

    I suggest you go back to the drawing board and try implementing
    the algorithm again.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    LCM
    ...
    lowest common denominator



    The easiest way to get the LCM: LCM is "least common multiple". So you start checking every number from 1 onwards until you find a number divisible by both X and Y. This is, by definition, the LEAST common multiple, since if there was a smaller one, you would have found it already. You will always find an LCM, because the largest possible value is X*Y.

    The faster and better way to get the LCM: Note that LCM(X,Y) == X*Y / GCD(X,Y). There is already a fast and easy algorithm to get the GCD -- check Richie T's link.


    Don't start coding yet... find out exactly what the problem is, and plan a way to solve it, before you actually put the thought into code.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. This is driving me nuts...
    By jesin in forum C Programming
    Replies: 5
    Last Post: 11-22-2005, 05:17 AM
  3. This is driving me nuts.
    By Matt13 in forum C Programming
    Replies: 3
    Last Post: 03-25-2004, 10:55 AM
  4. This compiler is driving me nuts
    By martin_00 in forum C Programming
    Replies: 32
    Last Post: 12-31-2002, 03:25 PM
  5. driving me nuts
    By boontune in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2002, 04:35 AM