/// <summary>Computes the nearest number greater than or equal to the /// specified start value that is coprime to another specified value. /// Returns 0 if no coprime was found in the range start to MaxValue.</summary> /// <param name="start">The start value.</param> /// <param name="value2">The value to test against.</param> /// <returns>The largest value that is less than or equal to the start value /// and also coprime to the other specified value.</returns> public static int NearestCoprimeCeiling(this int start, int value2) { uint v1, v2; v2 = (value2 >= 0) ? (uint)value2 : value2.AbsU(); if ((value2 == 0) && (start <= -1)) { return(-1); } if ((start == int.MaxValue) || ((value2 == 0) && (start > 1))) { return(0); } v1 = start.AbsU(); if (start >= 0) { while (!IsCoprime(v1, v2)) { if (v1 == int.MaxValue) { return(0); } v1++; } return((int)v1); } else { while (!IsCoprime(v1, v2)) { v1--; } return(-(int)v1); } }
/// <summary>Computes the greatest common divisor of two values.</summary> /// <param name="value1">The first value.</param> /// <param name="value2">The other value.</param> /// <returns>The greatest common divisor of the two values.</returns> public static int Gcd(this int value1, int value2) { if ((value1 | value2) < 0) { return((int)Gcd(value1.AbsU(), value2.AbsU())); } return((int)Gcd((uint)value1, (uint)value2)); }
/// <summary>Computes the nearest number less than or equal to the /// specified start value that is coprime to another specified value. /// Returns 0 if no coprime was found in the range MinValue to start.</summary> /// <param name="start">The start value.</param> /// <param name="value2">The value to test against.</param> /// <returns>The largest value that is less than or equal to the start value /// and also coprime to the other specified value.</returns> public static int NearestCoprimeFloor(this int start, int value2) { uint v1, v2; v2 = (value2 >= 0) ? (uint)value2 : value2.AbsU(); if (start > 0) { return((int)NearestCoprimeFloor((uint)start, v2)); } if ((value2 == 0) && (start < -1)) { return(0); } for (v1 = start.AbsU(); v1 <= (uint)(-(int.MinValue + 1)) + 1; v1++) { if (v1.IsCoprime(v2)) { return(-(int)v1); } } return(0); }