예제 #1
0
        /// <summary>
        /// Calculates binary expressions and pushes the result into the operands stack
        /// </summary>
        /// <param name="op">Binary operator</param>
        /// <param name="operand1">First operand</param>
        /// <param name="operand2">Second operand</param>
        private void Calculate(string op, PrimesBigInteger operand1, PrimesBigInteger operand2)
        {
            PrimesBigInteger res = PrimesBigInteger.Zero;

            try
            {
                switch (op)
                {
                case Token.Add: res = operand1.Add(operand2); break;

                case Token.Subtract: res = operand1.Subtract(operand2); break;

                case Token.Multiply: res = operand1.Multiply(operand2); break;

                case Token.Divide: res = operand1.Divide(operand2); break;

                case Token.Mod: res = operand1.Mod(operand2); break;

                case Token.Power: res = operand1.Pow(operand2.IntValue); break;

                case Token.Log: res = PrimesBigInteger.Zero; break;

                case Token.Root: res = PrimesBigInteger.Zero; break;
                }

                operands.Push(PostProcess(res));
            }
            catch (Exception e)
            {
                ThrowException(e.Message);
            }
        }
예제 #2
0
        private void DoFactorize(PrimesBigInteger value)
        {
            /*
             *        if (N.compareTo(ONE) == 0) return;
             * if (N.isProbablePrime(20)) { System.out.println(N); return; }
             * BigInteger divisor = rho(N);
             * factor(divisor);
             * factor(N.divide(divisor));
             */
            if (value.Equals(PrimesBigInteger.One))
            {
                return;
            }
            if (value.IsProbablePrime(10))
            {
                if (!m_Factors.ContainsKey(value))
                {
                    m_Factors.Add(value, PrimesBigInteger.Zero);
                }
                PrimesBigInteger tmp = m_Factors[value];
                m_Factors[value] = tmp.Add(PrimesBigInteger.One);

                if (FoundFactor != null)
                {
                    FoundFactor(m_Factors.GetEnumerator());
                }
                log.Info(value.ToString());
                return;
            }
            else
            {
                if (!m_FactorsTmp.ContainsKey(value))
                {
                    m_FactorsTmp.Add(value, 0);
                }
                m_FactorsTmp[value]++;
                if (m_FactorsTmp[value] > 3)
                {
                    log.Info(value.ToString() + " Zu oft");
                    m_A = PrimesBigInteger.RandomM(value).Add(PrimesBigInteger.Two);
                    m_FactorsTmp.Remove(value);
                }
            }
            PrimesBigInteger div = CalculateFactor(value);

            DoFactorize(div);
            DoFactorize(value.Divide(div));
        }
예제 #3
0
        private void TrialDivision()
        {
            m_Height = 0;
            PrimesBigInteger divisor = PrimesBigInteger.Two;

            FireOnActualDivisorChanged(divisor);
            PrimesBigInteger  value = new PrimesBigInteger(this.m_Root.Value);
            GmpFactorTreeNode node  = this.m_Root;

            while (!value.IsProbablePrime(10))
            {
                //int counter = 0;
                while (value.Mod(divisor).CompareTo(PrimesBigInteger.Zero) != 0)
                {
                    divisor = divisor.NextProbablePrime();
                    FireOnActualDivisorChanged(divisor);
                    //counter++;
                    //if (counter == 1000000)
                    //{
                    //  if (OnCanceled != null) OnCanceled("Nach 100.000 Versuchen wurde kein weiterer Faktor gefunden, das Verfahren wird abgebrochen.");
                    //  CancelFactorize();
                    //}
                }
                value       = value.Divide(divisor);
                m_Remainder = value;
                GmpFactorTreeNode primeNodeTmp = new GmpFactorTreeNode(divisor);
                primeNodeTmp.IsPrime = true;
                node.AddChild(primeNodeTmp);
                node         = node.AddChild(new GmpFactorTreeNode(value));
                node.IsPrime = value.IsProbablePrime(20);
                m_Height++;
                AddFactor(divisor);
            }
            m_Remainder = null;

            node.IsPrime = true;
            AddFactor(node.Value);
            if (OnStop != null)
            {
                OnStop();
            }
        }