Example #1
0
        private bool SameFactorization(FactorManager factorManager)
        {
            bool equalPrimes     = primeFactors.Intersect(factorManager.primeFactors).Count() == 0;
            bool equalComposites = compositeFactors.Intersect(factorManager.compositeFactors).Count() == 0;

            return(equalPrimes && equalComposites);
        }
Example #2
0
        /// <summary>
        /// replaces the composite factor "composite" by the factors of factorList.
        /// of course, factorList has to multiply up to composite.
        /// </summary>
        public void ReplaceCompositeByFactors(BigInteger composite, IntPtr factorList)
        {
            //Some debug stuff:
            FactorManager debugFactorManager = new FactorManager(getPrimeFactorsMethod, getCompositeFactorsMethod, composite);

            debugFactorManager.AddFactorsWithoutFiringEvent(factorList);
            Debug.Assert(debugFactorManager.CalculateNumber() == composite);

            //Add:
            AddFactorsWithoutFiringEvent(factorList);
            normalizeLists();

            Debug.Assert(CalculateNumber() == this.number);
            FactorsChanged(primeFactors, compositeFactors);
        }
Example #3
0
        /// <summary>
        /// uses the informations from the factorManager parameter to transform some composite factors to prime factors.
        /// returns true if this factorManager has more informations than the parameter factorManager.
        /// </summary>
        public bool Synchronize(FactorManager factorManager)
        {
            Debug.Assert(factorManager.CalculateNumber() == this.CalculateNumber());
            if (SameFactorization(factorManager))
            {
                return(false);
            }

            //check if we can gain information from factorManager for our FactorList (and put these informations in our list)
            foreach (BigInteger comp in compositeFactors)
            {
                if (!factorManager.compositeFactors.Contains(comp))
                {
                    List <BigInteger> primeFactorsForComp     = new List <BigInteger>();
                    List <BigInteger> compositeFactorsForComp = new List <BigInteger>();

                    //Let's check whether factorManager already has a factorization for comp:
                    foreach (BigInteger p in factorManager.primeFactors)
                    {
                        if (comp % p == 0)
                        {
                            primeFactorsForComp.Add(p);
                        }
                    }
                    foreach (BigInteger c in factorManager.compositeFactors)
                    {
                        if (comp != c && comp % c == 0)
                        {
                            compositeFactorsForComp.Add(c);
                        }
                    }

                    if (primeFactorsForComp.Count != 0 || compositeFactorsForComp.Count != 0)
                    {
                        ReplaceCompositeByFactors(comp, primeFactorsForComp, compositeFactorsForComp);
                        return(Synchronize(factorManager));
                    }
                }
            }

            //now check if our FactorList has more informations than factorManager:
            return(!SameFactorization(factorManager));
        }
Example #4
0
        /// <summary>
        /// Called by the environment to execute this plugin
        /// </summary>
        public void Execute()
        {
            if (checkInUse())
            {
                return;
            }

            try
            {
                Thread.CurrentThread.CurrentCulture   = _cultureInfo;
                Thread.CurrentThread.CurrentUICulture = _cultureUiInfo;

                if (useGnuplot)
                {
                    gnuplotFile = new StreamWriter(Path.Combine(directoryName, "gnuplot.dat"), false);
                }

                userStopped       = false;
                otherPeerFinished = false;

                if (InputNumber < 2)
                {
                    GuiLogMessage("Please enter a number >= 2.", NotificationLevel.Error);
                    return;
                }

                if (InputNumber.ToString().Length >= 275)
                {
                    GuiLogMessage(Resources.Input_too_big_, NotificationLevel.Error);
                    return;
                }

                String info_message = typeof(QuadraticSieve).GetPluginStringResource("Starting_quadratic_sieve");

                start_time = DateTime.Now;

                GuiLogMessage(info_message, NotificationLevel.Info);
                quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback) delegate
                {
                    quadraticSieveQuickWatchPresentation.ProgressRelationPackages.Clear();
                    quadraticSieveQuickWatchPresentation.information.Content = info_message;
                    quadraticSieveQuickWatchPresentation.endTime.Content     = "-";
                    quadraticSieveQuickWatchPresentation.timeLeft.Content    = "-";
                    quadraticSieveQuickWatchPresentation.elapsedTime.Content = "-";
                    quadraticSieveQuickWatchPresentation.startTime.Content   = "" + start_time;
                    quadraticSieveQuickWatchPresentation.factorList.Items.Clear();
                    quadraticSieveQuickWatchPresentation.factorInfo.Content = typeof(QuadraticSieve).GetPluginStringResource("Searching_trivial_factors");
                    if (usePeer2Peer)
                    {
                        quadraticSieveQuickWatchPresentation.localSieving.Visibility = Visibility.Hidden;
                    }
                    else
                    {
                        quadraticSieveQuickWatchPresentation.localSieving.Visibility = Visibility.Visible;
                    }
                }
                                                                       , null);

                initMsieveDLL();
                factorManager = new FactorManager(msieve.GetMethod("getPrimeFactors"), msieve.GetMethod("getCompositeFactors"), InputNumber);
                factorManager.FactorsChanged += this.FactorsChanged;

                //Now factorize:
                try
                {
                    string file = Path.Combine(directoryName, "" + InputNumber + ".dat");
                    if (settings.DeleteCache && File.Exists(file))
                    {
                        File.Delete(file);
                    }
                    MethodInfo start = msieve.GetMethod("start");
                    start.Invoke(null, new object[] { InputNumber.ToString(), file });
                    obj = IntPtr.Zero;
                }
                catch (Exception ex)
                {
                    GuiLogMessage("Error using msieve. " + ex.Message, NotificationLevel.Error);
                    stopThreads();
                    return;
                }

                if (!userStopped)
                {
                    Debug.Assert(factorManager.CalculateNumber() == InputNumber);
                    OutputFactors = factorManager.getPrimeFactors();

                    String timeLeft_message = Resources.NoneLeft;
                    String endtime_message  = DateTime.Now.ToString();

                    quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback) delegate
                    {
                        quadraticSieveQuickWatchPresentation.information.Content = string.Format(typeof(QuadraticSieve).GetPluginStringResource("Sieving_finished"), OutputFactors.Count());
                        quadraticSieveQuickWatchPresentation.endTime.Content     = endtime_message;
                        quadraticSieveQuickWatchPresentation.timeLeft.Content    = timeLeft_message;
                        quadraticSieveQuickWatchPresentation.factorInfo.Content  = "";
                    }
                                                                           , null);

                    ProgressChanged(1, 1);
                }
                else
                {
                    info_message = typeof(QuadraticSieve).GetPluginStringResource("Stopped_by_user");

                    GuiLogMessage(info_message, NotificationLevel.Info);
                    quadraticSieveQuickWatchPresentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback) delegate
                    {
                        quadraticSieveQuickWatchPresentation.information.Content = typeof(QuadraticSieve).GetPluginStringResource("Stopped_by_user");
                        quadraticSieveQuickWatchPresentation.endTime.Content     = "-";
                        quadraticSieveQuickWatchPresentation.timeLeft.Content    = "-";
                        quadraticSieveQuickWatchPresentation.startTime.Content   = "-";
                        quadraticSieveQuickWatchPresentation.elapsedTime.Content = "-";
                        quadraticSieveQuickWatchPresentation.factorInfo.Content  = "";
                    }
                                                                           , null);
                }

                if (useGnuplot)
                {
                    gnuplotFile.Close();
                }
            }
            finally
            {
                alreadyInUse = false;
            }
        }