博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python lcm()_Python LCM –找到LCM的2种方法
阅读量:2517 次
发布时间:2019-05-11

本文共 3359 字,大约阅读时间需要 11 分钟。

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=12y=20if 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: 

另请阅读:

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 mathdef get_lcm(n1,n2):  #find gcd  gcd = math.gcd(n1,n2)    #formula   result = (n1*n2)/gcd  return result  n1 = 12n2  = 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 gcdlist1 = [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程序相关的任何问题或建议,请在评论框中告诉我们。

翻译自:

python lcm()

转载地址:http://iaggb.baihongyu.com/

你可能感兴趣的文章
PHP Curl发送数据
查看>>
HTTP协议
查看>>
HTTPS
查看>>
git add . git add -u git add -A区别
查看>>
apache下虚拟域名配置
查看>>
session和cookie区别与联系
查看>>
PHP 实现笛卡尔积
查看>>
Laravel中的$loop
查看>>
CentOS7 重置root密码
查看>>
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
laravel 定时任务秒级执行
查看>>
浅析 Laravel 官方文档推荐的 Nginx 配置
查看>>
Swagger在Laravel项目中的使用
查看>>
Laravel 的生命周期
查看>>
CentOS Docker 安装
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>