如何求两个数的最小公倍数 (LCM)
我已经使用欧几里得的方法来找到两个数字的最小公倍数。
l.c.m=a*b/(gcd(a,b))
如果不使用这个算法我该如何做到这一点? 我的想法是首先获取这两个数字的所有因数并将它们存储在数组中。然后从数组 1 中取出 1 个元素并在数组 2 中搜索它,如果它存在,则从那里删除它并使结果乘以该数字。
这样可以吗?
I have used Euclid's method to find the L.C.M for two numbers.
l.c.m=a*b/(gcd(a,b))
How can I do this without using this algorithm?
I have an idea of first getting all factors of these two numbers and storing them in array. Then take 1 element from array 1 and search for it in array2, if it present there then remove it from there and make the result multiply by that num.
Is this OK?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
几乎。 4 和 8 的 LCM 是多少?显然是 8 (23),但在您的方法中您会发现 2。您不仅需要跟踪所有因素,还需要跟踪它们出现的频率。
Almost. What's the LCM of 4 and 8? Obviously 8 (23), but in your method you'd find 2. You need to keep track not just of all factors, but also how often they appear.
我相信您建议的算法是使用表格的方法,请检查看看它是否适合你。
I believe the algorithm you suggest is a method using a table, check to see if it works for you.
LCM(最小公倍数)始终大于或等于两个数字中的较大者。因此,我们首先检查较大的数字本身是否是两个数字的 LCM,方法是检查较大的数字是否可以被较小的数字整除,如果是,我们找到了 LCM 和 LCM。如果不是,那么我们会将较大的数字加 1 并再次检查。
LCM(Least Common Multiple) is always greater than or equal to the larger of the two numbers. So we will first check that the larger number itself a LCM of two numbers by checking that the larger number is divisible by the smaller one , if yes we found the LCM & If no then we will increment the larger number by 1 and check again.
**使用 while 循环的两个数字的 LCM
**
**LCM of two numbers using while loop
**
首先得到GCD就可以得到两个数的最小公倍数(LCM)。
这是上述问题的解决方案。
You can get LCM of two number by getting GCD at first.
Here is the solution for the above.