本文整理匯總了C#中BigInteger.Signum方法的典型用法代碼示例。如果您正苦於以下問題:C# BigInteger.Signum方法的具體用法?C# BigInteger.Signum怎麽用?C# BigInteger.Signum使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類BigInteger
的用法示例。
在下文中一共展示了BigInteger.Signum方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Jacobi
/// <summary>
/// Computes the value of the Jacobi symbol (A|B).
/// </summary>
///
/// <param name="A">The integer value</param>
/// <param name="B">The integer value</param>
///
/// <returns>Returns value of the jacobi symbol (A|B)</returns>
public static int Jacobi(BigInteger A, BigInteger B)
{
BigInteger a, b, v;
long k = 1;
// test trivial cases
if (B.Equals(ZERO))
{
a = A.Abs();
return a.Equals(ONE) ? 1 : 0;
}
if (!A.TestBit(0) && !B.TestBit(0))
return 0;
a = A;
b = B;
if (b.Signum() == -1)
{ // b < 0
b = b.Negate();
if (a.Signum() == -1)
k = -1;
}
v = ZERO;
while (!b.TestBit(0))
{
v = v.Add(ONE);
b = b.Divide(TWO);
}
if (v.TestBit(0))
k = k * _jacobiTable[a.ToInt32() & 7];
if (a.Signum() < 0)
{
if (b.TestBit(1))
k = -k;
a = a.Negate();
}
// main loop
while (a.Signum() != 0)
{
v = ZERO;
while (!a.TestBit(0))
{ // a is even
v = v.Add(ONE);
a = a.Divide(TWO);
}
if (v.TestBit(0))
k = k * _jacobiTable[b.ToInt32() & 7];
if (a.CompareTo(b) < 0)
{
// swap and correct intermediate result
BigInteger x = a;
a = b;
b = x;
if (a.TestBit(1) && b.TestBit(1))
k = -k;
}
a = a.Subtract(b);
}
return b.Equals(ONE) ? (int)k : 0;
}
示例2: NextProbablePrime
/// <summary>
/// Compute the next probable prime greater than <c>N</c> with the specified certainty
/// </summary>
///
/// <param name="X">An integer number</param>
/// <param name="Certainty">The certainty that the generated number is prime</param>
///
/// <returns>Returns the next prime greater than <c>N</c></returns>
public static BigInteger NextProbablePrime(BigInteger X, int Certainty)
{
if (X.Signum() < 0 || X.Signum() == 0 || X.Equals(ONE))
return TWO;
BigInteger result = X.Add(ONE);
// Ensure an odd number
if (!result.TestBit(0))
result = result.Add(ONE);
while (true)
{
// Do cheap "pre-test" if applicable
if (result.BitLength > 6)
{
long r = result.Remainder(BigInteger.ValueOf(SMALL_PRIME_PRODUCT)).ToInt64();
if ((r % 3 == 0) || (r % 5 == 0) || (r % 7 == 0) ||
(r % 11 == 0) || (r % 13 == 0) || (r % 17 == 0) ||
(r % 19 == 0) || (r % 23 == 0) || (r % 29 == 0) ||
(r % 31 == 0) || (r % 37 == 0) || (r % 41 == 0))
{
result = result.Add(TWO);
continue; // Candidate is composite; try another
}
}
// All candidates of bitLength 2 and 3 are prime by this point
if (result.BitLength < 4)
return result;
// The expensive test
if (result.IsProbablePrime(Certainty))
return result;
result = result.Add(TWO);
}
}
示例3: DivideAndRound
/// <summary>
/// Divide two BigIntegers and return the rounded result
/// </summary>
///
/// <param name="A">The first BigInteger</param>
/// <param name="B">The second BigInteger</param>
///
/// <returns>The rounded result</returns>
public static BigInteger DivideAndRound(BigInteger A, BigInteger B)
{
if (A.Signum() < 0)
return DivideAndRound(A.Negate(), B).Negate();
if (B.Signum() < 0)
return DivideAndRound(A, B.Negate()).Negate();
return A.ShiftLeft(1).Add(B).Divide(B.ShiftLeft(1));
}
示例4: ExtGcd
/// <summary>
/// Extended euclidian algorithm (computes Gcd and representation)
/// </summary>
///
/// <param name="A">The first BigInteger</param>
/// <param name="B">The second BigInteger</param>
///
/// <returns>Returns <c>(d,u,v)</c>, where <c>d = Gcd(A,B) = ua + vb</c></returns>
public static BigInteger[] ExtGcd(BigInteger A, BigInteger B)
{
BigInteger u = ONE;
BigInteger v = ZERO;
BigInteger d = A;
if (B.Signum() != 0)
{
BigInteger v1 = ZERO;
BigInteger v3 = B;
while (v3.Signum() != 0)
{
BigInteger[] tmp = d.DivideAndRemainder(v3);
BigInteger q = tmp[0];
BigInteger t3 = tmp[1];
BigInteger t1 = u.Subtract(q.Multiply(v1));
u = v1;
d = v3;
v1 = t1;
v3 = t3;
}
v = d.Subtract(A.Multiply(u)).Divide(B);
}
return new BigInteger[] { d, u, v };
}
示例5: InplaceShiftRight
/// <summary>
/// Performs Value >>= count where Value is a positive number.
/// </summary>
///
/// <param name="Value">The source BigIntger</param>
/// <param name="N">Shift distance</param>
internal static void InplaceShiftRight(BigInteger Value, int N)
{
int sign = Value.Signum();
if (N == 0 || Value.Signum() == 0)
return;
int intCount = N >> 5; // count of integers
Value._numberLength -= intCount;
if (!ShiftRight(Value._digits, Value._numberLength, Value._digits, intCount, N & 31) && sign < 0)
{
// remainder not zero: add one to the result
int i;
for (i = 0; (i < Value._numberLength) && (Value._digits[i] == -1); i++)
Value._digits[i] = 0;
if (i == Value._numberLength)
Value._numberLength++;
Value._digits[i]++;
}
Value.CutOffLeadingZeroes();
Value.UnCache();
}
示例6: Multiply
/// <summary>
/// Multiplies two BigIntegers using the Schönhage-Strassen algorithm.
/// </summary>
///
/// <param name="A">Factor A</param>
/// <param name="B">Factor B</param>
///
/// <returns>BigInteger equal to <c>A.Multiply(B)</c></returns>
public static BigInteger Multiply(BigInteger A, BigInteger B)
{
// remove any minus signs, multiply, then fix sign
int signum = A.Signum() * B.Signum();
if (A.Signum() < 0)
A = A.Negate();
if (B.Signum() < 0)
B = B.Negate();
int[] aIntArr = ToIntArray(A);
int[] bIntArr = ToIntArray(B);
int[] cIntArr = Multiply(aIntArr, A.BitLength, bIntArr, B.BitLength);
BigInteger c = ToBigInteger(cIntArr);
if (signum < 0)
c = c.Negate();
return c;
}
示例7: TestDiv
private void TestDiv(BigInteger i1, BigInteger i2)
{
BigInteger q = i1.Divide(i2);
BigInteger r = i1.Remainder(i2);
BigInteger[] q2 = i1.DivideAndRemainder(i2);
BigInteger quotient = q2[0];
BigInteger remainder = q2[1];
IsTrue(q.Equals(quotient), "Divide and DivideAndRemainder do not agree");
IsTrue(r.Equals(remainder), "Remainder and DivideAndRemainder do not agree");
IsTrue(q.Signum() != 0 || q.Equals(zero), "signum and equals(zero) do not agree on quotient");
IsTrue(r.Signum() != 0 || r.Equals(zero), "signum and equals(zero) do not agree on remainder");
IsTrue(q.Signum() == 0 || q.Signum() == i1.Signum() * i2.Signum(), "wrong sign on quotient");
IsTrue(r.Signum() == 0 || r.Signum() == i1.Signum(), "wrong sign on remainder");
IsTrue(r.Abs().CompareTo(i2.Abs()) < 0, "remainder out of range");
IsTrue(q.Abs().Add(one).Multiply(i2.Abs()).CompareTo(i1.Abs()) > 0, "quotient too small");
IsTrue(q.Abs().Multiply(i2.Abs()).CompareTo(i1.Abs()) <= 0, "quotient too large");
BigInteger p = q.Multiply(i2);
BigInteger a = p.Add(r);
IsTrue(a.Equals(i1), "(a/b)*b+(a%b) != a");
try
{
BigInteger mod = i1.Mod(i2);
IsTrue(mod.Signum() >= 0, "mod is negative");
IsTrue(mod.Abs().CompareTo(i2.Abs()) < 0, "mod out of range");
IsTrue(r.Signum() < 0 || r.Equals(mod), "positive remainder == mod");
IsTrue(r.Signum() >= 0 || r.Equals(mod.Subtract(i2)), "negative remainder == mod - divisor");
}
catch
{
IsTrue(i2.Signum() <= 0, "mod fails on negative divisor only");
}
}
示例8: ModInverseLorencz
private static BigInteger ModInverseLorencz(BigInteger X, BigInteger Modulo)
{
// Based on "New Algorithm for Classical Modular Inverse" Róbert Lórencz. LNCS 2523 (2002)
// PRE: a is coprime with modulo, a < modulo
int max = System.Math.Max(X._numberLength, Modulo._numberLength);
int[] uDigits = new int[max + 1]; // enough place to make all the inplace operation
int[] vDigits = new int[max + 1];
Array.Copy(Modulo._digits, 0, uDigits, 0, Modulo._numberLength);
Array.Copy(X._digits, 0, vDigits, 0, X._numberLength);
BigInteger u = new BigInteger(Modulo._sign, Modulo._numberLength, uDigits);
BigInteger v = new BigInteger(X._sign, X._numberLength, vDigits);
BigInteger r = new BigInteger(0, 1, new int[max + 1]); // BigInteger.ZERO;
BigInteger s = new BigInteger(1, 1, new int[max + 1]);
s._digits[0] = 1;
// r == 0 && s == 1, but with enough place
int coefU = 0, coefV = 0;
int n = Modulo.BitLength;
int k;
while (!IsPowerOfTwo(u, coefU) && !IsPowerOfTwo(v, coefV))
{
// modification of original algorithm: I calculate how many times the algorithm will enter in the same branch of if
k = HowManyIterations(u, n);
if (k != 0)
{
BitLevel.InplaceShiftLeft(u, k);
if (coefU >= coefV)
{
BitLevel.InplaceShiftLeft(r, k);
}
else
{
BitLevel.InplaceShiftRight(s, System.Math.Min(coefV - coefU, k));
if (k - (coefV - coefU) > 0)
BitLevel.InplaceShiftLeft(r, k - coefV + coefU);
}
coefU += k;
}
k = HowManyIterations(v, n);
if (k != 0)
{
BitLevel.InplaceShiftLeft(v, k);
if (coefV >= coefU)
{
BitLevel.InplaceShiftLeft(s, k);
}
else
{
BitLevel.InplaceShiftRight(r, System.Math.Min(coefU - coefV, k));
if (k - (coefU - coefV) > 0)
BitLevel.InplaceShiftLeft(s, k - coefU + coefV);
}
coefV += k;
}
if (u.Signum() == v.Signum())
{
if (coefU <= coefV)
{
Elementary.CompleteInPlaceSubtract(u, v);
Elementary.CompleteInPlaceSubtract(r, s);
}
else
{
Elementary.CompleteInPlaceSubtract(v, u);
Elementary.CompleteInPlaceSubtract(s, r);
}
}
else
{
if (coefU <= coefV)
{
Elementary.CompleteInPlaceAdd(u, v);
Elementary.CompleteInPlaceAdd(r, s);
}
else
{
Elementary.CompleteInPlaceAdd(v, u);
Elementary.CompleteInPlaceAdd(s, r);
}
}
if (v.Signum() == 0 || u.Signum() == 0)
throw new ArithmeticException("BigInteger not invertible");
}
if (IsPowerOfTwo(v, coefV))
{
r = s;
if (v.Signum() != u.Signum())
u = u.Negate();
}
if (u.TestBit(n))
{
//.........這裏部分代碼省略.........
示例9: GcdBinary
/// <summary>
/// Return the greatest common divisor of X and Y
/// </summary>
///
/// <param name="X">Operand 1, must be greater than zero</param>
/// <param name="Y">Operand 2, must be greater than zero</param>
///
/// <returns>Returns <c>GCD(X, Y)</c></returns>
internal static BigInteger GcdBinary(BigInteger X, BigInteger Y)
{
// Divide both number the maximal possible times by 2 without rounding * gcd(2*a, 2*b) = 2 * gcd(a,b)
int lsb1 = X.LowestSetBit;
int lsb2 = Y.LowestSetBit;
int pow2Count = System.Math.Min(lsb1, lsb2);
BitLevel.InplaceShiftRight(X, lsb1);
BitLevel.InplaceShiftRight(Y, lsb2);
BigInteger swap;
// I want op2 > op1
if (X.CompareTo(Y) == BigInteger.GREATER)
{
swap = X;
X = Y;
Y = swap;
}
do
{ // INV: op2 >= op1 && both are odd unless op1 = 0
// Optimization for small operands (op2.bitLength() < 64) implies by INV (op1.bitLength() < 64)
if ((Y._numberLength == 1) || ((Y._numberLength == 2) && (Y._digits[1] > 0)))
{
Y = BigInteger.ValueOf(Division.GcdBinary(X.ToInt64(), Y.ToInt64()));
break;
}
// Implements one step of the Euclidean algorithm
// To reduce one operand if it's much smaller than the other one
if (Y._numberLength > X._numberLength * 1.2)
{
Y = Y.Remainder(X);
if (Y.Signum() != 0)
BitLevel.InplaceShiftRight(Y, Y.LowestSetBit);
}
else
{
// Use Knuth's algorithm of successive subtract and shifting
do
{
Elementary.InplaceSubtract(Y, X); // both are odd
BitLevel.InplaceShiftRight(Y, Y.LowestSetBit); // op2 is even
} while (Y.CompareTo(X) >= BigInteger.EQUALS);
}
// now op1 >= op2
swap = Y;
Y = X;
X = swap;
} while (X._sign != 0);
return Y.ShiftLeft(pow2Count);
}