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 + 1print ("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 gcdgcd = math.gcd(n1,n2)#formula result = (n1*n2)/gcdreturn resultn1 = 12
n2  = 20lcm = 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()

python lcm()_Python LCM –找到LCM的2种方法相关推荐

  1. python图层_Python叠加矩形框图层2种方法及效果

    两种方式以及效果: 方式一,使用PIL.Image.blend方式: from PIL import Image, ImageDraw im = Image.open('d:/tmp/58.249.0 ...

  2. ubuntu编写python脚本_python在ubuntu中的几种方法(小结)

    通过ubuntu官方的apt工具包安装 通过PPA(Personal Package Archive) 的apt工具包安装 通过编译python源代码安装 通过ubuntu官方的apt工具包安装 安装 ...

  3. python怎么清屏_python实现清屏的方法 Python Shell中清屏一般有两种方法。

    Python Shell 怎样清屏? Python Shell中清屏一般有两种方法. 奈何一个人随着年龄增长,梦想便不复轻盈:他开始用双手掂量生活,更看重果实而非花朵.--叶芝<凯尔特的搏暮&g ...

  4. Python语言学习:利用python语言实现调用内部命令(python调用Shell脚本)—命令提示符cmd的几种方法

    Python语言学习:利用python语言实现调用内部命令(python调用Shell脚本)-命令提示符cmd的几种方法 目录 利用python语言实现调用内部命令-命令提示符cmd的几种方法 T1. ...

  5. python列表list元素降序排列两种方法

    python列表list元素降序排列的两种方法 sort()方法 python列表内置了一个sort()方法,可以用于为元素列表进行排序,当将默认参数reverse设置为True,sort()方法将为 ...

  6. python中字符串转成数字的几种方法

    在python列表操作中,面对需要把列表中的字符串转为礼拜的操作,无需强转,通过简单的几步就可以实现,本文介绍python中字符串转成数字的三种方法:1.使用join的方法:2.使用int函数将16进 ...

  7. python租车系统_使用Python实现租车计费系统的两种方法

    您的位置:首页 > 站长学院 > 网络技术    正文内容 使用Python实现租车计费系统的两种方法 使用Python实现租车计费系统的两种方法 更新时间:2018-09-29 18:0 ...

  8. Python实现将内容写入文件的五种方法总结

    本篇带你详细看一下python将内容写入文件的方法以及细节,主要包括write()方法.writelines() 方法.print() 函数.使用 csv 模块.使用 json 模块,需要的可以参考一 ...

  9. python if多条件并列判断的三种方法

    python if多条件并列判断的三种方法 如果使用python的if进行多个条件表达式的判断呢?下面介绍三种方法: 使用and或or来连接多个条件表达式,比如条件1 and 条件2 and条件3等等 ...

  10. 基于Python实现中文文本关键词抽取的三种方法 课程报告+项目源码及数据

    资源下载地址:https://download.csdn.net/download/sheziqiong/85737856 资源下载地址:https://download.csdn.net/downl ...

最新文章

  1. java修改jar包
  2. 怎样学好网络(1)-正确的定位
  3. 1.2.2 OSI参考模型
  4. python 只取年月日 字符串_Python的数据类型
  5. modelsim(1):经常使用的测试设计的结构
  6. 前端学习(2064):vue的生命周期函数有什么
  7. python怎么播放本地录音_Python播放音频与录音
  8. 实战系列-分布式锁的Redis实现
  9. python执行Linux系统命令
  10. SQL查询语句,怎样查询重复数据
  11. velocity mybatis spring 在maven的整合开发(四)
  12. Spring Boot(1) 入门、自动配置
  13. 新媒体运营的“钱途”在哪里?
  14. alwayson高可用组_AlwaysOn可用性组–如何在集群实例和独立实例之间设置AG(第2部分)
  15. 安卓桌面壁纸_安卓视频桌面哪个好用 让手机桌面更炫酷
  16. 为什么C++构造函数不能是虚函数
  17. 利用计算机及时采集检测数据,计算机基础_课件.ppt
  18. Linux操作系统之常用快捷键汇集
  19. 大一c语言作业操作题库,大学C语言考试题库(含答案)
  20. Fe3O4 NPs@MIL-53|NH2-MIL-53(Sc)|NH2-MIL-53(Al)|NH2-MIL-125负载银钴合金纳米材料|氨基化mof材料

热门文章

  1. 微信公众号授权登录、获取用户信息(openid)
  2. 双减政策下,研学旅行或将成为大赢家
  3. QGIS添加在线地图,谷歌地图,OSM地图
  4. Linux xampp apache启动失败
  5. U盘启动Openwrt写入硬盘教程
  6. 【uniapp-内置组件editor富文本编辑器】
  7. scrapy时尚网站onylady图片分类爬虫
  8. Window下完全卸载删除Nodejs
  9. matplotlib函数画出高数函数图像
  10. 实验4——门磁单实验