算法:歐幾里得求最大公約數(python版)


#歐幾里得求最大公約數 #!/usr/bin/env python #coding -*- utf:8 -*-

#iteration
def gcd(a,b): if b==0: return a else: return gcd(b, remainder(a, b)) #此方法僅僅書用於a和b都為正數
def gcd_1(a,b): while(b>0): rem = remainder(a,b) a = b b = rem return a def remainder(x,y): return x%y if __name__=='__main__': a = int(input("請輸入一個數字:")) b = int(input("請輸入另外一個數字:")) print("最大公約數:",gcd(a,b))

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM