python数组查找元素_Python程序查找数组元素的LCM

news/2024/5/19 22:17:15 标签: 算法, python, 数据结构, java, 快速排序

python数组查找元素

LCM is the lowest multiple of two or more numbers. Multiples of a number are those numbers which when divided by the number leave no remainder. When we are talking about the multiple, we consider only the positive number. For example, the LCM of 12 and 48 is 48 because 48 is 4th multiple of 12 and 48 is 1st multiple of 48. Here, an array of positive elements will be provided by the user and we have to find the LCM of the elements of the array by using the Python. To find LCM of the elements of the array, use the following algo,

LCM两个或多个数字最小倍数 。 数字的倍数是那些除以数字后没有余数的数字。 当我们谈论倍数时,我们只考虑正数。 例如,12和48的LCM是48,因为48是4 12 倍数和4848 1 倍数。 在这里,用户将提供一个正元素数组,我们必须使用Python查找该数组元素的LCM 。 要查找数组元素的LCM ,请使用以下算法

Algorithm to find the LCM of array elements

查找数组元素的LCM的算法

  • We need to import the math module to find the GCD of two numbers using math.gcd() function.

    我们需要导入math模块以使用math.gcd()函数查找两个数字的GCD。

  • At first, find the LCM of initial two numbers using: LCM(a,b) = a*b/GCD(a,b). And, then find the LCM of three numbers with the help of LCM of first two numbers using LCM(ab,c) = lcm(lcm(a1, a2), a3). The same concept we have implemented.

    首先,使用以下公式找到初始两个数字的LCM: LCM(a,b)= a * b / GCD(a,b) 。 然后,使用LCM(ab,c)= lcm(lcm(a1,a2),a3)在前两个数字的LCM的帮助下找到三个数字的LCM。 我们已经实现了相同的概念。

Now, we will write the Python program in a simple way by implementing the above algorithm.

现在,我们将通过实现上述算法以简单的方式编写Python程序。

Program:

程序:

# importing the module
import math

# function to calculate LCM
def LCMofArray(a):
  lcm = a[0]
  for i in range(1,len(a)):
    lcm = lcm*a[i]//math.gcd(lcm, a[i])
  return lcm


# array of integers
arr1 = [1,2,3]
arr2 = [2,3,4]
arr3 = [3,4,5]
arr4 = [2,4,6,8]
arr5 = [8,4,12,40,26,28]

print("LCM of arr1 elements:", LCMofArray(arr1))
print("LCM of arr2 elements:", LCMofArray(arr2))
print("LCM of arr3 elements:", LCMofArray(arr3))
print("LCM of arr4 elements:", LCMofArray(arr4))
print("LCM of arr5 elements:", LCMofArray(arr5))

Output

输出量

LCM of arr1 elements: 6
LCM of arr2 elements: 12
LCM of arr3 elements: 60
LCM of arr4 elements: 24
LCM of arr5 elements: 10920


翻译自: https://www.includehelp.com/python/find-the-lcm-of-the-array-elements.aspx

python数组查找元素


http://www.niftyadmin.cn/n/1255381.html

相关文章

大小端,主机序,网络序

大端模式(Big_endian):字数据的高字节存储在低地址中,而字数据的低字节则存放在高地址中。 小端模式(Little_endian):字数据的高字节存储在高地址中,而字数据的低字节则存放在低地址…

python字符ascii_Python程序打印字符的ASCII值

python字符asciiGiven a character, and we have to find its ASCII code. 给定一个字符,我们必须找到它的ASCII码。 Python中字符的ASCII值 (ASCII value of character in Python) In python, to get an ASCII code of a character, we use ord() function. ord()…

Redis学习笔记(三):Redis常见命令

KEY相关命令: DEL key : 该命令用于key存在时删除keyDUMP key : 序列化给定key,并返回被序列化的值EXISTS key :检测key是否存在EXPIRE key seconds : 为给定的key设置过期时间EXPIREAT key timestamp :…

atoi的简易实现

atoi() ——字符串转数字 //暂时仅考虑了十进制和十六进制 int atoi(const char * buf) {int value 0;int base 10;int i 0;if (buf[0] 0 && buf[1] x){base 16;i 2;}while (buf[i]){int tmp;if (buf[i] < 9 && buf[i] > 0){tmp buf[i] -0;}else{t…

RISC-V平台的汇编指令解析

csrr a0, 0xF14 //把0xF14的值读入到a0中 andi a1, a0, 0x1f //把a0 和0x1F按位与运算后存储到a1中 srli a0, a0, 5 //将高位移动到低位,覆盖a0 (SLLI是逻辑左移(0被移入低位); SRLI是逻辑右移(0被移入高位);SRAI是算术右移(原来的符号位被复制到空出的高位中)) li …

海报PLA (HYSBZ - 1113)单调栈模板

N个矩形,排成一排. 现在希望用尽量少的矩形海报Cover住它们. Input 第一行给出数字N,代表有N个矩形.N在[1,250000] 下面N行,每行给出矩形的长与宽.其值在[1,1000000000]2 1/2 Postering Output 最少数量的海报数. Sample Input5 1 21 32 22 51 4 Sample Output 4 先解释下题意&…

Java中从String到Float的转换

Given a string and we have to convert it into a float. 给定一个字符串&#xff0c;我们必须将其转换为浮点数。 Java conversion from String to Float Java从String转换为Float To convert a String to Float, we can use the following methods of Float class (see th…

django基础篇06-ModelForm操作及验证

本文内容主要来自银角大王的博客 学习大纲&#xff1a; 一、ModelForm  二、Ajax- 原生&#xff08;jQuery&#xff09;- 伪Ajax操作三、文件上传&#xff08;预览&#xff09;- Form提交- Ajax文件上传四、 图片验证码 session 五、CKEditor、UEEditor、TinyEditor、KindEd…