当前位置: 首页>>代码示例>>C#>>正文


C# Numerics.BigInteger类代码示例

本文整理汇总了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);
            }

        }
开发者ID:SerKalayder,项目名称:Csharp-code,代码行数:33,代码来源:Program.cs

示例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());
 }
开发者ID:bityuan,项目名称:AntShares,代码行数:7,代码来源:ScriptBuilder.cs

示例3: RetrievePrivateKey

 public static BigInteger RetrievePrivateKey(string path)
 {
     var bytes = File.ReadAllBytes(@"" + path);
     var privateKey = new BigInteger(bytes);
     Console.WriteLine("D: " + privateKey);
     return privateKey;
 }
开发者ID:Dagothversur,项目名称:RSA-cryptosystem-,代码行数:7,代码来源:KeysRetriever.cs

示例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;
        }
开发者ID:b-slavov,项目名称:Telerik-Software-Academy,代码行数:27,代码来源:Election.cs

示例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();
            }
        }
开发者ID:OVME,项目名称:vich_sistemy,代码行数:29,代码来源:Factorial.cs

示例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;
        }
开发者ID:DVitinnik,项目名称:UniversityApps,代码行数:31,代码来源:Diffi-Hellman.cs

示例7: DiffieHellman

        public DiffieHellman(BigInteger prime, BigInteger generator)
        {
            Prime = prime;
            Generator = generator;

            Initialize(true);
        }
开发者ID:weslley17w,项目名称:Yupi,代码行数:7,代码来源:DiffieHellman.cs

示例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;
            }
        }
开发者ID:weslley17w,项目名称:Yupi,代码行数:33,代码来源:DiffieHellman.cs

示例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);
        }
开发者ID:brunoss,项目名称:Toolbox,代码行数:7,代码来源:MathUtils.cs

示例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());
        }
开发者ID:modulexcite,项目名称:LoreSoft.Shared,代码行数:38,代码来源:BaseConvert.cs

示例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;
        }
开发者ID:nickstark,项目名称:FibonacciPro,代码行数:33,代码来源:Calculator.cs

示例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;
            }
        }
开发者ID:wpq0,项目名称:Document-Signer,代码行数:35,代码来源:OfficeOpenXmlSigner.cs

示例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;
            }
开发者ID:Sihd,项目名称:FactorTree,代码行数:31,代码来源:Factor.cs

示例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;
        }
开发者ID:vhst,项目名称:Prime_Numbers_Generator,代码行数:32,代码来源:Generator.cs

示例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]);
        }
开发者ID:Ivan-Dimitrov-bg,项目名称:.Net-framework,代码行数:28,代码来源:SumFibonacci.cs


注:本文中的System.Numerics.BigInteger类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。