python lcm()_Python LCM –找到LCM的2种方法

本文介绍了在Python中求最小公倍数(LCM)的两种方法:使用循环和使用最大公约数(GCD)。提供了详细的代码示例,包括处理多个数字的情况。
Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

python lcm()

In this article, we’ll see different ways to find LCM in Python with program examples.

在本文中,我们将通过程序示例介绍在Python中查找LCM的不同方法。

Basically LCM is a smallest number that is divisible by both numbers (or all). Let us see how we can find lcm of two numbers in python.

基本上,LCM是可被两个(或全部)数字整除的最小数字。 让我们看看如何在python中找到两个数字的lcm。

1. Using Loop

1.使用循环

First we find the larger number of two given numbers. Starting from it and will try to find the first number that is divisible by both, which is LCM.

首先,我们找到两个给定数字更大的数字。 从它开始,将尝试找到可被两者整除的第一个数字,即LCM。

x=12
y=20
if x > y:  
   greater = x  
else:  
   greater = y  
while(True):  
   if((greater % x == 0) and (greater % y == 0)):  
        lcm = greater  
        break  
   greater =  greater + 1
 
print ("Least common multiple = ", lcm)

Output:

输出:

Least common multiple =  60

最小公倍数= 60

In above program, first we’re finding greater number, then start a loop. Inside the loop we’ll find a number that can be divisible by both of given numbers n1 and n2. When we will get a that number we’ll store it into a new variable called lcm. If didn’t get then we’ll increase greater by 1. As we know the number will be greater than both of the numbers that why we’re start  checking lcm from greater number.

在上面的程序中,首先我们找到更大的数字,然后开始循环。 在循环内,我们将找到一个可以被给定数字n1和n2整除的数字。 当我们得到一个数字时,我们会将其存储到一个名为lcm的新变量中 如果没有得到,我们将增加1。正如我们所知道的,该数字将大于两个数字,这就是为什么我们要从更大的数字开始检查lcm的原因。

2. Using GCD

2.使用GCD

If you’ve the basic knowledge of mathematics, then you would know that we can find LCM very easily using GCD.

如果您具有数学的基本知识,那么您将知道我们可以使用GCD轻松找到LCM。

Also Read: Python GCD – 4 Ways to Find GCD or HCF

另请阅读: Python GCD –查找GCD或HCF的4种方法

Here is the formula:

这是公式:

number 1 * number 2 =  LCM * GCD

1号* 2号= LCM * GCD

So,

所以,

LCM  =   (number 1 * number 2)/GCD of number1 and 2

LCM =(数字1 *数字2)/数字1和2的GCD

Let’s implement this formula into program.

让我们将此公式实现到程序中。

import math
def get_lcm(n1,n2):
  #find gcd
  gcd = math.gcd(n1,n2)
  
  #formula 
  result = (n1*n2)/gcd
  return result
  
n1 = 12
n2  = 20
 
lcm = get_lcm(n1,n2)
print("least common multiple  =  ", lcm)

Output

输出量

least common multiple = 60.0

最小公倍数= 60.0

So in above program we’ve have function that receives two arguments and then inside it, first we’ll find GCD and after that we’re applying given formula to find out LCM with the help of GCD and return it.

因此,在上面的程序中,我们有接收两个参数的函数,然后在其中,首先找到GCD,然后使用给定的公式借助GCD找出LCM并将其返回。

So these were two easiest ways to get the LCM of two given numbers. But what if we have more than two numbers. So here is the program for it.

因此,这是获得两个给定数字的LCM的两种最简单方法。 但是如果我们有两个以上的数字怎么办。 所以这是它的程序。

How to find LCM of more than two numbers?

如何找到两个以上的LCM?

from math import gcd
list1 = [12,48,8,60]   
lcm = list1[0]
for i in list1[1:]:
  lcm = int(lcm*i/gcd(lcm, i))
print("least common multiple =  ", lcm)

Output:

输出:

least common multiple =  240

最小公倍数= 240

So in above program, we have an list of numbers and then we will store first item of the list in variable lcm. Then we will loop through all the elements present in the list1. Inside the loop we’ll multiply lcm with i/GCD of lcm and i. So after breaking the loop we’ll get our LCM of all numbers in variable lcm.

因此,在上面的程序中,我们有一个数字列表,然后将列表的第一项存储在变量lcm中。 然后,我们将遍历list1中存在的所有元素。 在循环内部,我们将lcm与lcm和i的i / GCD相乘 因此,在打破循环之后,我们将获得所有数字的LCM(可变lcm)。

If you’ve any problem or suggestion related to python lcm programs then please let us know in comment box.

如果您有与python lcm程序相关的任何问题或建议,请在评论框中告诉我们。

翻译自: https://www.thecrazyprogrammer.com/2018/05/python-lcm-2-ways-to-find-lcm.html

python lcm()

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值