这段程序第六行红色德那句, 怎么会返回三个数? # function for extended Euclidean Algorithm def gcdExtended(a, b): # Base Case if a == 0: return b, 0, 1 gcd, x1, y1 = gcdExtended(b % a, a) # Update x and y using results of recursive # call x = y1 - (b//a) * x1 y = x1 return gcd, x, y
Answer from chatgpt This is an implementation of the extended Euclidean algorithm, which is an algorithm that takes in two positive integers a and b and returns the greatest common divisor of a and b, as well as the coefficients x and y such that ax + by = gcd(a, b). The algorithm works by recursively applying the Euclidean algorithm to find the greatest common divisor of a and b, but also keeps track of the coefficients x and y as it goes. The base case is when a is equal to 0, in which case the greatest common divisor is b and the coefficients x and y are 0 and 1, respectively. Otherwise, the algorithm recursively computes the greatest common divisor of b % a and a, as well as the coefficients x1 and y1. It then uses these values to compute the coefficients x and y using the equation x = y1 - (b//a) * x1 and y = x1. Finally, it returns the greatest common divisor, x, and y.
这段程序第六行红色德那句, 怎么会返回三个数? # function for extended Euclidean Algorithm def gcdExtended(a, b): # Base Case if a == 0: return b, 0, 1 gcd, x1, y1 = gcdExtended(b % a, a) # Update x and y using results of recursive # call x = y1 - (b//a) * x1 y = x1 return gcd, x, y majia666 发表于 2022-12-21 20:10
# function for extended Euclidean Algorithm def gcdExtended(a, b): # Base Case if a == 0: return b, 0, 1 gcd, x1, y1 = gcdExtended(b % a, a) # Update x and y using results of recursive # call x = y1 - (b//a) * x1 y = x1 return gcd, x, y
那个GCD 的值是从哪里算出来的?
递归调用阿, base case 是 第一个 return , a == 0.
这种带 共产党 (gcd) 的 面试一般不会考, 哈哈。
好像有个 library function 可以 call.
高中小孩作业。我不会PYTHON, 但基本概念还是有。
这个function不就返回了三个值吗?你这里不就是调用了这个function?
我还以为你在刷题呢, 哈哈。 连三个返回值都看不出来, 就不要吹了。 LOL
辗转相除法,递归算出来的。
基本概念你就没有
你别辅导了,你远不如你小孩
我猜楼主就是看等号右边了。。。
看了您的问题,我感觉您只接触过C/C++/Java,肯定不懂Fortran。