本文整理汇总了C#中System.Numerics.BigInteger类的典型用法代码示例。如果您正苦于以下问题:C# BigInteger类的具体用法?C# BigInteger怎么用?C# BigInteger使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BigInteger类属于System.Numerics命名空间,在下文中一共展示了BigInteger类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Read
/// Read long numbers and initialize parameters
/// A0 - The right particle of the first part of the number
/// A1 - The left particle of the first part of the number
/// B0 - The right particle of the second part of the number
/// B1 - The left particle of the second part of the number
/// K - Middle of the numbers
/// N - Number of bits
public static void Read(out BigInteger A0, out BigInteger A1,
out BigInteger B0, out BigInteger B1,
out int K, out int N)
{
using (StreamReader sr = new StreamReader("test.txt"))
{
string a, a0, a1, b, b0, b1;
a = sr.ReadLine();
b = sr.ReadLine();
K = a.Length / 2;
N = a.Length;
// making strings to parse
a0 = a.Substring(0, K);
a1 = a.Substring(K);
b0 = b.Substring(0, K);
b1 = b.Substring(K);
A0 = BigInteger.Parse(a1);
A1 = BigInteger.Parse(a0);
B0 = BigInteger.Parse(b1);
B1 = BigInteger.Parse(b0);
}
}
示例2: Push
public ScriptBuilder Push(BigInteger number)
{
if (number == -1) return Add(ScriptOp.OP_1NEGATE);
if (number == 0) return Add(ScriptOp.OP_0);
if (number > 0 && number <= 16) return Add(ScriptOp.OP_1 - 1 + (byte)number);
return Push(number.ToByteArray());
}
示例3: RetrievePrivateKey
public static BigInteger RetrievePrivateKey(string path)
{
var bytes = File.ReadAllBytes(@"" + path);
var privateKey = new BigInteger(bytes);
Console.WriteLine("D: " + privateKey);
return privateKey;
}
示例4: Solve
private static BigInteger Solve(List<int> parties, int k)
{
var sums = new BigInteger[parties.Sum() + 1];
sums[0] = 1;
var maxSum = 0;
for (int i = 0; i < parties.Count; i++)
{
var number = parties[i];
for (int j = maxSum; j >= 0; j--)
{
if (sums[j] > 0)
{
sums[j + number] += sums[j];
maxSum = Math.Max(j + number, maxSum);
}
}
}
BigInteger combinations = 0;
for (int i = k; i <= parties.Sum(); i++)
{
combinations += sums[i];
}
return combinations;
}
示例5: Execute
public void Execute()
{
//это сам алгоритм
_factorial = 1;
for (int i = _number1; i < _number2 + 1; i++)
{
_factorial *= i;
}
Data = _factorial.ToString();
if (/*_myClientNumber < _highestClientNumber &&*/ _myClientNumber != 0)
{
var incomingPackage = _internalPackageProvider.GetPackage();
var firstMult = BigInteger.Parse(incomingPackage.Data);
_factorial *= firstMult;
}
if (_myClientNumber < _highestClientNumber)
{
formPackage(_factorial.ToString(),_myClientNumber+1);
OnSend();
}
if (_myClientNumber == _highestClientNumber)
{
formPackage(_factorial.ToString(), -1);
OnReady();
}
}
示例6: GeneratePG
public void GeneratePG()
{
byte[] bytes = new byte[KeySize / 8];
BigInteger p = new BigInteger(bytes);
RandomNumberGenerator rng = RandomNumberGenerator.Create();
while (!p.IsProbablePrime(KeySize))
{
rng.GetBytes(bytes);
byte[] temp = new byte[bytes.Length + 1];
Array.Copy(bytes, temp, bytes.Length);
p = new BigInteger(temp);
}
bytes = new byte[KeySize / 8];
BigInteger g = new BigInteger(bytes);
while (!g.IsProbablePrime(KeySize) || g >= p)
{
rng.GetBytes(bytes);
byte[] temp = new byte[bytes.Length + 1];
Array.Copy(bytes, temp, bytes.Length);
g = new BigInteger(temp);
}
P = p;
G = g;
}
示例7: DiffieHellman
public DiffieHellman(BigInteger prime, BigInteger generator)
{
Prime = prime;
Generator = generator;
Initialize(true);
}
示例8: Initialize
private void Initialize(bool ignoreBaseKeys = false)
{
PublicKey = 0;
Random rand = new Random();
while (PublicKey == 0)
{
if (!ignoreBaseKeys)
{
Prime = PrimeCalculator.GenPseudoPrime(Bitlength, 10, rand);
Generator = PrimeCalculator.GenPseudoPrime(Bitlength, 10, rand);
}
byte[] bytes = new byte[Bitlength/8];
Randomizer.NextBytes(bytes);
_privateKey = new BigInteger(bytes);
if (_privateKey < 1)
continue;
if (Generator > Prime)
{
BigInteger temp = Prime;
Prime = Generator;
Generator = temp;
}
PublicKey = BigInteger.ModPow(Generator, _privateKey, Prime);
if (!ignoreBaseKeys)
break;
}
}
示例9: IsSqrt
private static bool IsSqrt(BigInteger n, BigInteger root)
{
var lowerBound = root * root;
var upperBound = (root + 1) * (root + 1);
return (n >= lowerBound && n < upperBound);
}
示例10: ToBaseString
/// <summary>
/// Converts a <see cref="BigInteger"/> to its equivalent string representation that is encoded with base digits specified.
/// </summary>
/// <param name="value">A <see cref="BigInteger"/> that will be encoded.</param>
/// <param name="baseDigits">The base digits used to encode with.</param>
/// <returns>The string representation, in base digits, of the contents of <paramref name="value"/>.</returns>
public static string ToBaseString(BigInteger value, string baseDigits)
{
if (baseDigits == null)
throw new ArgumentNullException("baseDigits");
if (baseDigits.Length < 2)
throw new ArgumentOutOfRangeException("baseDigits", "Base alphabet must have at least two characters.");
// same functionality as Convert.ToBase64String
if (value == BigInteger.Zero)
return string.Empty;
bool isNegative = value.Sign == -1;
value = isNegative ? -value : value;
int length = baseDigits.Length;
var result = new Stack<char>();
do
{
BigInteger remainder;
value = BigInteger.DivRem(value, length, out remainder);
result.Push(baseDigits[(int)remainder]);
} while (value > 0);
// if the number is negative, add the sign.
if (isNegative)
result.Push('-');
// reverse it
return new string(result.ToArray());
}
示例11: Compute
public FibonacciResultSet Compute(int len)
{
if (len < 0)
{
throw new ArgumentOutOfRangeException();
}
var results = new BigInteger[len];
// zero case
if (len == 0)
{
return new FibonacciResultSet { Results = results };
}
// one case
results[0] = 0;
if (len == 1)
{
return new FibonacciResultSet { Results = results };
}
// compute values
results[1] = 1;
for (int i = 2; i < len; i++)
{
results[i] = results[i - 1] + results[i - 2];
}
FibonacciResultSet resultSet = new FibonacciResultSet { Results = results };
return resultSet;
}
示例12: Verify
/// <summary>
/// Verify.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="serial">The serial.</param>
/// <returns></returns>
public override bool Verify(Stream input, string serial = null)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
using (var package = Package.Open(input, FileMode.Open, FileAccess.Read))
{
var mgr = new PackageDigitalSignatureManager(package)
{
CertificateOption = CertificateEmbeddingOption.InSignaturePart
};
var result = false;
foreach (var sig in mgr.Signatures)
{
var verifyResult = mgr.VerifySignatures(true);
result = verifyResult == VerifyResult.Success;
if (result && !String.IsNullOrWhiteSpace(serial))
{
var actualSerial = new BigInteger(sig.Signer.GetSerialNumber());
var expectedSerial = CertUtil.HexadecimalStringToBigInt(serial);
result = actualSerial == expectedSerial;
}
}
package.Close();
return result;
}
}
示例13: orderDigits
//prioritize digits at digit depth
private Digit[] orderDigits(int depth)
{
Digit[] d = new Digit[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
BigInteger[] products = new BigInteger[10];
BigInteger[] ordered = new BigInteger[10];
for(int i = 0; i < 10; i++)
{
index[depth] = (Byte)d[i];
setNums(depth);
products[i] = nums[0].Value * nums[1].Value;
}
for (int i = 0; i < 10; i++)
ordered[i] = products[i];
Array.Sort(ordered);
for (int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
if (products[i] == ordered[j])
d[i] = j;
}
}
return d;
}
示例14: IsPrime
/// <summary>
/// Determines whether BigInteger is prime number with probability that can be greater than requested probability
/// </summary>
/// <param name="number">The BigInteger to check</param>
/// <param name="probability">Probability of error</param>
/// <returns></returns>
public bool IsPrime(BigInteger number, BigNumDec probability)
{
if (probability.Equals(0) || probability.Equals(1))
throw new ArgumentException("Probability must be in range (0; 1)");
int iterations = 1;
BigNum baseNum = 0.25;
while (true)
{
if (baseNum <= probability)
break;
baseNum *= 0.25;
iterations++;
}
while (iterations > 0)
{
if (!IsPrime(number))
return false;
--iterations;
}
return true;
}
示例15: Main
static void Main(string[] args)
{
string titel = "SumFibonacci";
string problem = @"Write a program that reads a number N and calculates the sum of the first N members of the sequence of Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, …
1. Each member of the Fibonacci sequence (except the first two) is a sum of the previous two members.";
Console.WriteLine("Title: " + titel + "\n" + "Problem: " + problem);
int N;
Console.Write("Please enter N (N > 0):");
bool isNint = int.TryParse(Console.ReadLine(), out N);
BigInteger[] arr = new BigInteger[N + 1];
arr[0] = 1;
arr[1] = 1;
if (isNint && N > 0)
{
for (int i = 2; i < N; i++)
{
arr[i] = arr[i - 2] + arr[i - 1];
}
}
else
{
Console.WriteLine("Invalide input");
}
Console.WriteLine("The {0} Fibonacci member is: {1}", N, arr[N - 1]);
}