Euclid的Python算法? [英] Euclid's Algorithm in Python?
本文介绍了Euclid的Python算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在基础算法(计算机编程的艺术)中,讨论的第一个
算法是Euclid的算法。
我唯一的想法在python中写这个是它必须涉及
使用模数%符号。
如何在python中写这个?
In Fundamental Algorithms (The Art of Computer Programming), the first
algorithm discussed is Euclid''s Algorithm.
The only idea I have of writing this in python is that it must involve
usage of the modulo % sign.
How do I write this in python?
推荐答案
好吧,这篇文章
http://pythonjournal.cognizor.com/py...rithms-V1.html
是google上的第一个热门产品''''euclid'的算法 python''。
它包含这个函数:
def GCD(a,b):
断言a> = b#a必须是较大的数字
而(b!= 0):
余数= a%b
a,b = b,剩余
返回
杰夫
-----开始PGP SIGNATURE -----
版本:GnuPG v1.2.6(GNU / Linux)
iD8DBQFC8tQQJd01MZaTXX0RAo6iAJ9JUzRbQ4Cvthgp9 + pr3M HGiER / tACgl5rQ
bbHUVwSg5xRPhWdRAINbMTQ =
= Ah8K
----- END PGP SIGNATURE -----
Well, this article
http://pythonjournal.cognizor.com/py...rithms-V1.html
was the first hit on google for ''"euclid''s algorithm" python''.
It contains this function:
def GCD(a,b):
assert a >= b # a must be the larger number
while (b != 0):
remainder = a % b
a, b = b, remainder
return a
Jeff
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
iD8DBQFC8tQQJd01MZaTXX0RAo6iAJ9JUzRbQ4Cvthgp9+pr3M HGiER/tACgl5rQ
bbHUVwSg5xRPhWdRAINbMTQ=
=Ah8K
-----END PGP SIGNATURE-----
引发断言错误对于< b有点矫枉过正,因为它实际上不是输入错误的情况。所以通常你看到Euclid就像
这样:
def gcd(a,b):#函数的所有小写都是多一点
常规。
如果a< b:
a,b = b,a#确保a> = b交换a和b如果nessecary
而b!= 0:#注意括号在这里是unnessecary在python中
a,b = b,a%b
返回
更简洁,更具可读性(IMO) )。
如果你真的想检查实际的不良输入,你最好还是试试,例如,a和b都大于0 ....
je****@unpythonic.net 写道:
Raising an assertion error for a < b is a bit of overkill, since its
not really a case of bad input. So normally you see Euclid done like
this:
def gcd(a,b): # All lowercase for a function is a bit more
conventional.
if a < b:
a, b = b, a # Ensures a >= b by swapping a and b if nessecary
while b != 0: # Note parentheses are unnessecary here in python
a, b = b, a % b
return a
A bit more concise and no less readable (IMO).
If you really want to check for actual bad input, youre better off
testing, for example, than a and b are both greater than 0....
je****@unpythonic.net wrote:
好吧,这篇文章
http:// pythonjournal .cognizor.com / py ... rithms-V1.html
是谷歌第一次点击'''euclid'的算法" python''。
它包含这个功能:
def GCD(a,b):
断言a> = b#a必须是更大的数字
while(b!= 0):
其余= a%b
a,b = b,余数
返回
杰夫
Well, this article
http://pythonjournal.cognizor.com/py...rithms-V1.html
was the first hit on google for ''"euclid''s algorithm" python''.
It contains this function:
def GCD(a,b):
assert a >= b # a must be the larger number
while (b != 0):
remainder = a % b
a, b = b, remainder
return a
Jeff
>
Op 2005-08-05,Jordan Rastrick schreef< jr ******* @ student.usyd.edu.au>:
Op 2005-08-05, Jordan Rastrick schreef <jr*******@student.usyd.edu.au>:
提高<的断言错误b有点矫枉过正,因为它确实不是输入错误的情况。所以通常你会看到Euclid就像
这样:
def gcd(a,b):#函数的所有小写都是传统的。
如果a< b:
a,b = b,a#确保a> = b通过交换a和b如果nessecary
而b!= 0:#注意括号在python中是不可靠的
a ,b = b,a%b
返回
更简洁,同样可读(IMO)。
Raising an assertion error for a < b is a bit of overkill, since its
not really a case of bad input. So normally you see Euclid done like
this:
def gcd(a,b): # All lowercase for a function is a bit more
conventional.
if a < b:
a, b = b, a # Ensures a >= b by swapping a and b if nessecary
while b != 0: # Note parentheses are unnessecary here in python
a, b = b, a % b
return a
A bit more concise and no less readable (IMO).
if测试是不必要的。如果a小于b,那么两个值
将由while body交换。
-
Antoon Pardon
The if test is unnecessary. Should a be smaller than b, the two values
will be swapped by the while body.
--
Antoon Pardon
这篇关于Euclid的Python算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文