Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中最大公约数的代码_Python - Fatal编程技术网

Python中最大公约数的代码

Python中最大公约数的代码,python,Python,a和b的最大公约数(GCD)是将二者除掉而不带余数的最大数 找到两个数字的GCD的一种方法是欧几里德算法,它基于这样一种观察:如果r是a除以b的余数,那么GCD(a,b)=GCD(b,r)。作为基本情况,我们可以使用gcd(a,0)=a 编写一个名为gcd的函数,该函数接受参数a和b,并返回它们的最大公约数。这是一个函数 Python 2.7中inspect模块的源代码: >>> print inspect.getsource(gcd) def gcd(a, b): "

a和b的最大公约数(GCD)是将二者除掉而不带余数的最大数

找到两个数字的GCD的一种方法是欧几里德算法,它基于这样一种观察:如果
r
a
除以
b
的余数,那么
GCD(a,b)=GCD(b,r)
。作为基本情况,我们可以使用
gcd(a,0)=a

编写一个名为gcd的函数,该函数接受参数
a
b
,并返回它们的最大公约数。

这是一个函数

Python 2.7中
inspect
模块的源代码:

>>> print inspect.getsource(gcd)
def gcd(a, b):
    """Calculate the Greatest Common Divisor of a and b.

    Unless b==0, the result will have the same sign as b (so that when
    b is divided by it, the result comes out positive).
    """
    while b:
        a, b = b, a%b
    return a
从Python3.5开始,
gcd
分数
中的一个已弃用。此外,
inspect.getsource
不再返回两种方法的解释性源代码。

此代码根据用户给出的选择计算两个以上数字的gcd,此处用户给出了数字
价值交换对我来说不起作用。所以我只是为在ab中输入的数字设置了一个镜像情况:

def gcd(a, b):
    if a > b:
        r = a % b
        if r == 0:
            return b
        else:
            return gcd(b, r)
    if a < b:
        r = b % a
        if r == 0:
            return a
        else:
            return gcd(a, r)

print gcd(18, 2)
def gcd(a,b): 如果a>b: r=a%b 如果r==0: 返回b 其他: 返回gcd(b,r) 如果a
一种基于欧几里德算法的不同方法。

使用m-n的算法可能运行很长时间

def gcd(m,n):
    return gcd(abs(m-n), min(m, n)) if (m-n) else n
这一款性能更好:

def gcd(x, y):
    while y != 0:
        (x, y) = (y, x % y)
    return x

在使用递归的python中:

def gcd(a, b):
    if a%b == 0:
        return b
    return gcd(b, a%b)
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a%b)
对于
a>b

这个版本的代码使用欧几里德算法来寻找GCD

def gcd_recursive(a, b):
    if b == 0:
        return a
    else:
        return gcd_recursive(b, a % b)
#此程序将查找给定数字列表的hcf。
A=[65,20,100,85,125]#创建并初始化数字列表
定义最大公约数(_A):
迭代器=1
系数=1
a_长度=len(_a)
最小值=99999
#得到最小的数字
对于_A中的数字:#遍历数组
如果编号<最小值:#如果当前编号不是最小编号
最小=数字#设置为最高

而迭代器我认为另一种方法是使用递归。这是我的密码:

def gcd(a, b):
    if a > b:
        c = a - b
        gcd(b, c)
    elif a < b:
        c = b - a
        gcd(a, c)
    else:
        return a
def gcd(a,b): 如果a>b: c=a-b gcd(b、c) 如果a
使用递归的非常简洁的解决方案:

def gcd(a, b):
    if a%b == 0:
        return b
    return gcd(b, a%b)
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a%b)

使用递归

def gcd(a,b):
    return a if not b else gcd(b, a%b)
def gcd(a,b):
  while b:
    a,b = b, a%b
  return a
在时使用

def gcd(a,b):
    return a if not b else gcd(b, a%b)
def gcd(a,b):
  while b:
    a,b = b, a%b
  return a
使用lambda

gcd = lambda a,b : a if not b else gcd(b, a%b)

>>> gcd(10,20)
>>> 10

下面是实现迭代概念的解决方案:

def gcdIter(a, b):
    '''
    a, b: positive integers

    returns: a positive integer, the greatest common divisor of a & b.
    '''
    if a > b:
        result = b
    result = a

    if result == 1:
        return 1

    while result > 0:
        if a % result == 0 and b % result == 0:
            return result
        result -= 1

我不得不用while循环来完成家庭作业。这不是最有效的方法,但如果您不想使用某个函数,则可以:

num1 = 20
num1_list = []
num2 = 40
num2_list = []
x = 1
y = 1
while x <= num1:
    if num1 % x == 0:
        num1_list.append(x)
    x += 1
while y <= num2:
    if num2 % y == 0:
        num2_list.append(y)
    y += 1
xy = list(set(num1_list).intersection(num2_list))
print(xy[-1])
num1=20
num1_列表=[]
num2=40
num2_列表=[]
x=1
y=1


欢迎使用堆栈溢出!你会考虑添加一些叙述来解释这个代码为什么工作,什么使它成为问题的答案?这对提出问题的人和其他任何人都很有帮助。当你想比较平等时,千万不要使用“是”。小整数缓存是一个CPython实现细节。这非常慢,甚至不是有效的Python语法。缩进很重要。a=b时呢?你应该有一个初始的IF条件来捕捉它。这也是标准库中的条件。这个算法是如何工作的?这就像魔术一样。@netom:不,作业不能这样写;元组赋值在赋值之前使用
x
。您首先将
y
分配给
x
,因此现在
y
将被设置为
0
(因为
y%y
始终为0)。@MartijnPieters是的,您是对的,我应该使用一个临时变量。像这样:x_uu=y;y=x%y;x=x@netom:当使用这个答案中的元组赋值时,根本不需要它。它不会返回“将两个元组都除掉而没有余数的最大数”,例如,
分数。gcd(1,-1)
-1
,但
1>-1
,即。,
1
1
-1
分开,没有余数,并且大于
-1
,请参见@J.F.Sebastian我不认为这是一个问题。。。只要看看源代码中的注释:“除非b==0,否则结果将与b具有相同的符号”,因此
gcd(1,-1)=-1
对我来说似乎完全合法。@MarcoBonelli:是的。它的行为如文件所述,但它不是大多数人熟悉的教科书定义。就个人而言,我喜欢
分数.gcd()
原样(它适用于欧几里德环元素)。@J.F.Sebastian FWIW,从Python 3.5开始,
math.gcd(1,-1)
返回
1
@A-B-B math.gcd()和分数.gcd()与答案和评论中所说的不同。您在名称中使用了iter,但实际上它是一个递归版本。递归与循环版本相比效率很低,+您需要使用b>a
def gcd(a,b)调用它:如果b==0:return a return gcd(b,a%b)
python中的swap vars是儿童游戏:
b,a=a,b
。我喜欢你说的话,但我不喜欢你说话的方式。这是最简单的方法……别让它变得困难!感谢您提供可能有助于解决问题的代码,但通常情况下,如果答案中包含对代码的用途以及解决问题的原因的解释,则会更有帮助。此代码不完整(无最终返回语句)且格式不正确(无缩进)。我甚至不知道那条
break
语句想要实现什么。试试'np.gcd.reduce'在递归调用之后你不会返回。。。试着运行
gcd(10,5)
。@rem我认为“if not b”部分确实可以作为结束条件。
def gcdIter(a, b):
gcd= min (a,b)
for i in range(0,min(a,b)):
    if (a%gcd==0 and b%gcd==0):
        return gcd
        break
    gcd-=1
def gcd(a,b):
    return a if not b else gcd(b, a%b)
def gcd(a,b):
  while b:
    a,b = b, a%b
  return a
gcd = lambda a,b : a if not b else gcd(b, a%b)

>>> gcd(10,20)
>>> 10
def gcdIter(a, b):
    '''
    a, b: positive integers

    returns: a positive integer, the greatest common divisor of a & b.
    '''
    if a > b:
        result = b
    result = a

    if result == 1:
        return 1

    while result > 0:
        if a % result == 0 and b % result == 0:
            return result
        result -= 1
num1 = 20
num1_list = []
num2 = 40
num2_list = []
x = 1
y = 1
while x <= num1:
    if num1 % x == 0:
        num1_list.append(x)
    x += 1
while y <= num2:
    if num2 % y == 0:
        num2_list.append(y)
    y += 1
xy = list(set(num1_list).intersection(num2_list))
print(xy[-1])
def _grateest_common_devisor_euclid(p, q):
    if q==0 :
        return p
    else:
        reminder = p%q
        return _grateest_common_devisor_euclid(q, reminder)

print(_grateest_common_devisor_euclid(8,3))