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;
        }
    }
}
 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值