python gcd_Python程序来查找数组的GCD

python gcd

GCD of two or more non-zero number is the largest number that divides both or more non-zero numbers. It is one of the basic concepts of mathematics.

两个或多个非零数字的GCD是将两个或多个非零数字相除的最大数字。 它是数学的基本概念之一。

Example:

例:

    Input : 
    8, 4

    Output : 
    4 

    Explanation:
    8 and 4 are two non-zero numbers which are divided by 2 and 
    also by 4 but 4 is the largest number than 2. So, the GCD of 8 and 4 is 4. 

Here, an array of the non-zero numbers will be provided by the user and we have to find the GCD of the array elements in Python. To solve this problem, we will use the math module of Python. It is one of the most useful modules of Python used for mathematical operations. So, before going to solve this we will learn how to find the GCD of two non-zero numbers.

这里,用户将提供非零数字的数组,我们必须在Python中找到数组元素GCD 。 为了解决这个问题,我们将使用Python的math模块。 它是用于数学运算的Python最有用的模块之一。 因此,在解决此问题之前,我们将学习如何找到两个非零数字的GCD。

Python program to find the GCD of two non-zero numbers

Python程序查找两个非零数字的GCD

# importing the module
import math

# input two numbers
m,n=map(int,input('Enter two non-zero numbers: ').split())

#to find GCD
g=math.gcd(m,n) 

# printing the result
print('GCD of {} and {} is {}.'.format(m,n,g))

Output

输出量

Run 1: 
Enter two non-zero numbers: 8 4
GCD of 8 and 4 is 4.

Run 2:
Enter two non-zero numbers: 28 35
GCD of 28 and 35 is 7.

Now, we have learned to find the GCD of two non-zero number but our main task is to find the GCD of an array element or more than two non-zero numbers. So, let's go to write a Python program by simply using the above concepts.

现在,我们学会了找到两个非零数字的GCD,但是我们的主要任务是找到一个数组元素或两个以上非零数字的GCD。 因此,让我们简单地使用上述概念来编写Python程序。

Python program to find the GCD of the array

Python程序来查找数组的GCD

# importing the module
import math

# array of integers
A=[40,15,25,50,70,10,95]

#initialize variable b as first element of A
b=A[0]  
for j in range(1,len(A)):
    s=math.gcd(b,A[j])
    b=s
print('GCD of array elements is  {}.'.format(b))

Output

输出量

GCD of array elements is 5. 


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

python gcd

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值