Ai
1 Star 1 Fork 0

AndyZhang/C-Sharp-Algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
GreatestCommonDivisor.cs 1.09 KB
一键复制 编辑 原始数据 按行查看 历史
Ivandro Ismael 提交于 2018-05-21 07:50 +08:00 . remove redundant "else" / "else if"
/***
* Euclidean Algorithm to find the greatest common divisor of two numbers.
*
*/
namespace Algorithms.Numeric
{
public static class GreatestCommonDivisor
{
/// <summary>
/// Finds and returns the greatest common divisor of two numbers
/// </summary>
public static uint FindGCD(uint a, uint b)
{
if (a == 0)
return b;
if (b == 0)
return a;
uint _a = a, _b = b;
//Bitwise operator '&' works on individual bits of each value
//result is 0 or 1
//it works like a modulus operator '%' but is more efficient
uint r = _a & _b;
while(r != 0)
{
_a = _b;
_b = r;
r = _a & _b;
}
return _b;
}
/// <summary>
/// Determines given two numbers are relatively prime
/// </summary>
public static bool IsRelativelyPrime(uint a, uint b)
{
return FindGCD(a, b) == 1;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/strongandyzhang/C-Sharp-Algorithms.git
git@gitee.com:strongandyzhang/C-Sharp-Algorithms.git
strongandyzhang
C-Sharp-Algorithms
C-Sharp-Algorithms
master

搜索帮助