?
Solved

getting lowest and greatet common factors

Posted on 2011-10-03
4
Medium Priority
?
381 Views
Last Modified: 2012-05-12
Hi,

I think that this is an errata, but I want to check this with you guyz.

I have a method called HasComFactor() method to get the lowest and greatest common factors if they exist.
The original code is in the below.  
However, I have a problem with
int max = x < y ? x : y;

I think that it should be
int max = x < y ? y : x;

For 3 and 21,
it cannot find the lcf and gcf with 'int max = x < y ? x : y;', so I think that this is an error.

Am I right?

class MyClass{
    public bool HasComFactor(int x, int y, out int lcf, out int gcf)
    {
        int max = x < y ? x : y;
        bool first = true;

        lcf = 1;
        gcf = 1;

        // Find lcf and gcf
        for (int i = 2; i < max/2 + 1; i++)
        {
            if(((y%i)==0) & ((x%i)==0))
            {
                if(first)
                {
                    lcf = i;
                    first = false;
                }
                gcf = i;
            }
        }// for
        if(lcf !=1)
            return true;
        else
            return false;
    }
}// MyClass

class demo{    
    static void Main()
    {
        MyClass ob = new MyClass();

        int lcf, gcf;
        if(ob.HasComFactor(3, 21, out lcf, out gcf))
        {
            Console.WriteLine("LCF of 3 and 21 is " + lcf);
            Console.WriteLine("GCF of 3 and 21 is " + gcf);
        }
        else
            Console.WriteLine("No common factor for 231, 105");    
        Console.Read();
    }
}
0
Comment
Question by:IzzyTwinkly
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
pivar earned 1200 total points
ID: 36903382
Hi,

Yes, to get max you should use  x < y ? y : x

Try this:

	class MyClass {
		public bool HasComFactor(int x, int y, out int lcf, out int gcf)
		{
			int max = x < y ? y : x;
			bool first = true;

			lcf = 0;
			gcf = 0;

			// Find lcf and gcf
			for (int i = 1; i < max/2 + 1; i++)
			{
				if(((y%i)==0) & ((x%i)==0))
				{
					if(first)
					{
						lcf = i;
						first = false;
					}
					gcf = i;
				}
			}// for
			if(lcf != 0)
				return true;
			else
				return false;
		}
	}// MyClass

Open in new window


/peter
0
 
LVL 16

Assisted Solution

by:ToddBeaulieu
ToddBeaulieu earned 200 total points
ID: 36903842
You can also just use the framework math functions! It certainly makes for cleaner code (easier to understand) and you can use the more advanced functions when need be, as well.

int max = System.Math.Max(x, yield);
0
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 600 total points
ID: 36904020
The code has an error. If x and y are 25 and 50, then technically, the GCF is 25.
So you need to test for this.
Also, the original was right codewise and was just poorly named. There is no need to test any numbers greater than min/2 (other than min itself). So the variable should have been named min, not max.

My fixes are below
class MyClass {
  public bool HasComFactor(int x, int y, out int lcf, out int gcf)
  {
    int max = x > y ? x : y;
    int min = x < y ? x : y;
    bool foundLCF = false;
    bool foundGCF = false;

    lcf = 0;
    gcf = 0;
    
    if(max%min == 0)
    {
      foundGCF = true;
      gcf = min;
    }

    // Find lcf and gcf
    for (int i = 2; i < max/2 + 1 && (!foundGCF || !foundLCF); i++)
    {
      if(((y%i)==0) & ((x%i)==0))
      {
        if(!foundLCF)
        {
          lcf = i;
          foundLCF = true;
        }
        if(!foundGCF)
          gcf = i;
      }
    }// for
    if(lcf != 0)
      return true;
    else
      return false;
  }
}// MyClass 

Open in new window


Also, since you are not considering 1 to count as the LCF, pivar's code needs to start at 2 as well. But 1 is always the LCF, so perhaps the description needs to be modified.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36904025
Oops. On line 19 replace max with min.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Fluent Interface Design Pattern You can use the Fluent Interface (http://en.wikipedia.org/wiki/Fluent_interface) design pattern to make your PHP code easier to read and maintain.  "Fluent Interface" is an object-oriented design pattern that r…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question