Find the least common multiple

I got a question for interview and shared here.

Given two numbers m and n, write a method to return the first number r that is divisible by both(e.g., the least common multiple).


The Approach: what does it mean for r to be divisible by m and n? It means that all the primes in m must go into r, and all primes in n must be in r. What if m and n have primes in common? For example, if m is divisible by 3^5 and n is divisible by 3^7, what does this mean about r? It means r must be divisible by 3^7.

The rule: For each prime p such that p^a \ m(e.g., m is divisible by p^a) and p^b \ n, r must be divisible by p^max(a, b).


The Algorithm:


Define q to be 1.

for each prime number p less than m and n:

find the largest a and b such that p^a \ m or p^b \n

let q = q * p^max(a, b)

return q;


This is algoriithm is not good for code release, it may cause the code complex, time complex, space cmplex rising up.

So other ways to solve this problem has many. Form example: 1. you can use the one number m mutiply the number p from 2 to the another number n, if the result is divisbe by another number n, then you got the result. 2. You can the greatest common divisor first, then get the least common multple by the greates common divisor.


The code in C#:

using System;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            // call the function to get the least common multiple
            int commMultiple = GetLeastCommonMultiple(300, 210);

            Console.WriteLine(string.Format("result is {0}; ", commMultiple));
        }

        /// <summary>
        /// Get the least common multiple, return -1 if m and n is not valid number this function.
        /// </summary>
        /// <param name="m">first number</param>
        /// <param name="n">second number</param>
        /// <returns>the least common multiple</returns>
        public static int GetLeastCommonMultiple(int m, int n)
        {
            if (m < 0 || n < 0)
            {
                return -1;
            }

            int q = 1;
            for (int i = 2; i <= Math.Max(m, n); i++)
            {
                if (IsPrime(i))
                {
                    Console.Write(string.Format("i = {0}; ", i));
                    int p = 0;
                    for (int j = 0; j <= Math.Max(Math.Log(m, i), Math.Log(n, i)); j++)
                    {
                        if (m % Math.Pow(i, j) == 0 || n % Math.Pow(i, j) == 0)
                        {
                            p = j;
                            Console.Write(string.Format(" p = {0}", p));
                        }
                    }

                    Console.Write(";");

                    q *= (int)Math.Pow(i, p);
                    Console.WriteLine(string.Format("q = {0}; ", q));
                }               
            }

            return q;
        }

        /// <summary>
        /// Check the Prime
        /// </summary>
        /// <param name="n">the number needed check</param>
        /// <returns>the checked result</returns>
        public static bool IsPrime(int n)
        {
            if (n < 2)
            {
                throw new Exception();
            }

            for (int i = 2; i <= Math.Sqrt(n); i++)
            {
                if (n % i == 0)
                {
                    return false;
                }
            }

            return true;
        }
    }
}


欢迎来我的小站参观一折购(http://www.ezhegou.cn)




猜你喜欢

转载自blog.csdn.net/lwc5411117/article/details/44310255
今日推荐