/// <summary> /// The classic GCD algorithm of Euclid /// </summary> /// <param name="smaller"></param> /// <param name="larger"></param> /// <returns></returns> internal BigInteger Gcd(BigInteger smaller, BigInteger larger) { BigInteger rest; smaller = smaller.Abs(); larger = larger.Abs(); if (smaller == 0) { return(larger); } else { if (larger == 0) { return(smaller); } } // the GCD algorithm requires second >= first if (smaller > larger) { rest = larger; larger = smaller; smaller = rest; } while ((rest = larger % smaller) != 0) { larger = smaller; smaller = rest; } return(smaller); }
/// <summary> /// The classic GCD algorithm of Euclid /// </summary> /// <param name="smaller"></param> /// <param name="larger"></param> /// <returns></returns> internal BigInteger Gcd(BigInteger smaller, BigInteger larger) { if (smaller.IsInt && larger.IsInt) { return(Gcd((uint)smaller, (uint)larger)); } if (smaller.IsLong && larger.IsLong) { return(Gcd((ulong)smaller, (ulong)larger)); } BigInteger rest; smaller = smaller.Abs(); larger = larger.Abs(); if (smaller == 0) { return(larger); } else { if (larger == 0) { return(smaller); } } // the GCD algorithm requires second >= first if (smaller > larger) { rest = larger; larger = smaller; smaller = rest; } while ((rest = larger % smaller) != 0) { larger = smaller; smaller = rest; } return(smaller); }
/// <summary> /// The classic GCD algorithm of Euclid /// </summary> /// <param name="smaller"></param> /// <param name="larger"></param> /// <returns></returns> internal BigInteger Gcd(BigInteger smaller, BigInteger larger) { BigInteger rest; smaller = smaller.Abs(); larger = larger.Abs(); if (smaller == 0) return larger; else { if (larger == 0) return smaller; } // the GCD algorithm requires second >= first if(smaller > larger) { rest = larger; larger = smaller; smaller = rest; } while((rest = larger%smaller) != 0) { larger = smaller; smaller = rest; } return smaller; }