功能以在两个数字之间获得最高的共同除数

发布于 2025-01-29 21:50:18 字数 338 浏览 2 评论 0原文

我正在尝试实现一个函数,以在两个数字之间获得最高的公共除数,但是我会得到一个错误模棱两可的变量出现“ GCD”

gcd a 0 = 0
gcd 0 a = 0
gcd a b = if a > b
    then (if mod a b == 0 then a else gcd a (b-1))
    else (if mod b a == 0 then b else gcd (a-1) b)

如何执行它: 1-找到两个数字中最大的 2-最大的mod最小;如果它等于零,那么它将返回最小的,否则
它将再次调用方法,但使用(最小-1),最大的值相同

I am trying to implement a function to get the highest common divisor between two numbers, but I am getting an error Ambiguous variable occurrence "gcd".

gcd a 0 = 0
gcd 0 a = 0
gcd a b = if a > b
    then (if mod a b == 0 then a else gcd a (b-1))
    else (if mod b a == 0 then b else gcd (a-1) b)

How it will be executed:
1- find the greatest of two numbers
2- then the greatest mod smallest; if it equals zero then it will return the smallest, otherwise
it will call method again but with (smallest - 1) and the same value for the greatest

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文