两种计算lcm的方法之间的差异

时间:2019-03-20 04:56:36

标签: python

我遇到了两种计算lcm的方法。第一种方法使用递归,而第二种方法使用from tkinter import * from tkinter import filedialog from tkinter import messagebox def forButton1(): if MyVar1.get()==1: filename1 = filedialog.askopenfilename(initialdir="C:/DSSAT47/Soil") with open(filename1) as f1: for i in f1: myList1.insert(END, i) print(filename1) def forButton2(): if MyVar2.get() == 1: filename2 = filedialog.askopenfilename(initialdir="C:/DSSAT47/Weather") with open(filename2) as f2: for i in f2: myList1.insert(END, i) print(filename2) def forClear1(): MyVar1.set(0) print("*****x file removed*****") def forClear2(): MyVar2.set(0) print("*****y file removed*****") def forReset(): myList1.delete(0, 'end') MyVar1.set(0) MyVar2.set(0) print("*****All files removed*****") print("Select new files:") def forSubmit(): if MyVar1.get()== 0 or MyVar2.get()==0: messagebox.showwarning("Warning", "Select input files!") def forButton7(): if MyVar1.get()==0 and MyVar2.get()==0: messagebox.showwarning("Warning", "Select input files!") else: filename3 = filedialog.askopenfilename(initialdir="F:/ISRO/Python codes") with open(filename3) as f3: for i in f3: myList.insert(END, i) print(filename3) def forExit(): root.destroy() root = Tk() root.title("Spatialization of DSSAT") root.grid_columnconfigure(0, weight=1) topFrame = LabelFrame(root, text="Select input file") topFrame.grid(row=0, column=0, padx=8, pady=8, sticky=N+E+S+W) topFrame.grid_rowconfigure(0, weight=1) topFrame.grid_rowconfigure(1, weight=1) topFrame.grid_columnconfigure(0, weight=1) topFrame.grid_columnconfigure(1, weight=1) topFrame.grid_columnconfigure(2, weight=1) middleFrame = LabelFrame(root, text="Input data") middleFrame.grid(row=1, column=0, padx=8, pady=8, sticky=N+E+S+W) middleFrame.grid_rowconfigure(0, weight=1) middleFrame.grid_rowconfigure(1, weight=0) middleFrame.grid_columnconfigure(0, weight=1) middleFrame.grid_columnconfigure(1, weight=1) bottomFrame = LabelFrame(root, text="Model Output") bottomFrame.grid(row=2, column=0, padx=8, pady=8, sticky=N+E+S+W) bottomFrame.grid_rowconfigure(0, weight=1) bottomFrame.grid_columnconfigure(0, weight=1) MyVar1 = IntVar() MyVar2 = IntVar() MyCheckbutton1 = Checkbutton(topFrame, text="x", variable=MyVar1) MyCheckbutton1.grid(row=0, column=0, padx=4, pady=4) Button1 = Button(topFrame, text="Choose xFile", command=forButton1) Button1.grid(row=0, column=1, padx=4, pady=4) #Button3 = Button(topFrame, text="Clear", command=forClear1) #Button3.grid(row=0, column=2, padx=4, pady=4) MyCheckbutton2 = Checkbutton(topFrame, text="y", variable=MyVar2) MyCheckbutton2.grid(row=1, column=0, padx=4, pady=4) Button2 = Button(topFrame, text="Choose yFile", command=forButton2) Button2.grid(row=1, column=1, padx=4, pady=4) #Button4 = Button(topFrame, text="Clear", command=forClear2) #Button4.grid(row=1, column=2, padx=4, pady=4) innerMiddleFrame = Frame(middleFrame) innerMiddleFrame.grid(row=0, column=0, columnspan=2, padx=4, pady=4) innerMiddleFrame.grid_columnconfigure(0, weight=1) innerMiddleFrame.grid_columnconfigure(1, weight=0) scrollbar = Scrollbar(innerMiddleFrame) myList1 = Listbox(innerMiddleFrame, yscrollcommand=scrollbar.set) myList1.grid(row=0, column=0, sticky=N+E+S+W) scrollbar.config(command=myList1.yview) scrollbar.grid(row=0, column=1, sticky=N+E+S+W) Button5 = Button(middleFrame, text="Reset", command=forReset) Button5.grid(row=1, column=0, padx=4, pady=4) #Button6 = Button(middleFrame, text="Submit", command=forSubmit) #Button6.grid(row=1, column=1, padx=4, pady=4) innerBottomFrame = Frame(bottomFrame) innerBottomFrame.grid(row=0, column=0, columnspan=2, padx=4, pady=4) innerBottomFrame.grid_columnconfigure(0, weight=1) innerBottomFrame.grid_columnconfigure(1, weight=0) scrollbar = Scrollbar(innerBottomFrame) myList = Listbox(innerBottomFrame, yscrollcommand=scrollbar.set) myList.grid(row=0, column=0, sticky=N+E+S+W) scrollbar.config(command=myList.yview) scrollbar.grid(row=0, column=1, sticky=N+E+S+W) Button7 = Button(bottomFrame, text="Select Output File", command=forButton7) Button7.grid(row=1, column=0, padx=4, pady=4) Button8 = Button(bottomFrame, text="Exit", command=forExit) Button8.grid(row=1, column=1, padx=4, pady=4) root.geometry("400x590") import geopandas as gpd #from geopandas.io.file import read_file fp = r"F:\ISRO\Spatial_Data\grid_jagalur_spatialjoin.shp" data = gpd.read_file(fp) print (type(data)) data.head() data.plot() import matplotlib.pyplot as plt plt.show() root.mainloop() 运算符。我什至观察到,如果存在深度递归,则第一种方法会引发错误。除了在方法2中使用插入符号之外,我认为还必须有一种模式或一些直观的逻辑。如果是这样,您能给我解释一下吗?

方法1

caret

方法2

def gcd(a,b): 

 if (a == b): 
    return a 

 if (a > b): 
    return gcd(a-b, b) 

 return gcd(a, b-a) 


def lcm(a,b): 
   return (a*b) / gcd(a,b) 

1 个答案:

答案 0 :(得分:1)

使用3个XOR操作只是交换两个变量的愚蠢方式。

a ^= b
b ^= a
a ^= b

相同
a,b = b,a

这两种方法使用不同的计算方法,但是通过计算较大数除以较小数的余数,两者都以相同的方式收敛。递归方法使用多次减法以获得模的等价物,而迭代方法直接使用模运算符。递归方法将执行更多的迭代,因此对于相距很远的数字效率会大大降低。

将模运算符与递归方法结合使用将使两个函数成为单行定义:

def gcd(a,b): return a if b == 0 else gcd(b,a%b)
def lcm(a,b): return (a*b)//gcd(a,b) 

顺便说一下,这些是用于计算GCD的欧几里得算法的实现:https://en.wikipedia.org/wiki/Euclidean_algorithm