Optimal string algorithm for prime numbers:

Source: Internet
Author: User
Tags join split square root trim


Optimal string algorithm for prime numbers:

            string is divisor = "2", divisor = "2", after divisor = "2", Output = "", min. = string.
            Join ("\ r \ n", read text record (@System. environment.currentdirectory + "\ \ for prime number \ \ min.")); list<string> Read divisor = Read text record (@System. environment.currentdirectory + "\ \ \ Dividend. txt"), prime file set = Read text record (@ System.Environment.CurrentDirectory + "\ \ for prime number \ \ prime file set. txt"), read mass set = new List<string> (), read Max root = new list<string&
            gt; (); String[] Read Prime file set = Directory.GetFiles (@System. environment.currentdirectory + "\ \ For prime number", "Mass set *.txt",
            SEARCHOPTION.TOPDIRECTORYONLY);//Read text record (@System. environment.currentdirectory + "\ \ for prime number \ \ mass set. txt"); Prime number File set. AddRange (read prime number File set. Select (processing = = processing. Replace (System.Environment.CurrentDirectory + \ \ for prime number \ \ ""). Replace (". txt", "")).
            ToList ()); Prime number File Set = prime number File set. Distinct ().
            ToList (); Write change record (string.
            Join ("\ r \ n", prime number File set), "prime number", "Prime Document Set", "", false);
            
            int quality order = 0, root order = 0, Maximum root = 0, prime number file = 0, read file = 1, output = 0; func<string, string> promotion = Delegate (string increment) {return increment = infinite number of digits added (increment, increment = = "2"?)
            "1": "2") [0];
            }; Action Increment quantity = Delegate () {while (read mass set. Count <= maximum radical) read mass set. [0] AddRange (read text record (@System. environment.currentdirectory + "\ \ \" + prime number File Set [Read file + +] + ". txt"). Trim (). Split (').
            ToList ());
            };
                Action forward = delegate () {divisor = "2";
                After divisor = "2";
                Quality order = 0;
            by divisor = promotion (dividend);
            }; Action read-In data = Delegate () {prime number file volume = Prime file set.
                Count; if (prime number file volume = = 1)/*/File not divided by */{read Mass set = Read text record (@System. environment.currentdirectory + "\ \ for prime numbers \
                    \ "+ prime number File Set [0] +". txt "); if (read mass number set.) Count > 0) Read mass number set = read mass number set [0]. Trim (). Split (').
                ToList (); } if (prime number file Volume > 1) {Read qualityNumber set.
                    Clear ();
                Increase the quantity of quality ();
                Read the maximum root = read the text record (@System. environment.currentdirectory + "\ \ for prime number \ \ max.. txt"); Maximum radical = read the maximum root. Count > 0? Int. Parse (Read max. [1]): Read the mass number set.
            Count-1;
            };
            Read-in data (); if (read by divisor.)
            Count > 0) is divisor = enhanced (read dividend [0]); if (maximum root > 1) Root order = maximum radical;//= read mass set.

            LastIndexOf (read the maximum);
                do {string quotient = infinite number of bits divided (dividend, divisor, "1");
                String quotient after = infinite number of digits divided (dividend, divisor, "1"); BOOL Positive quotient = quotient. Contains ("."), positive quotient after = quotient.
                Contains (".");
                        if (divisor! = Dividend && divisor after! = Dividend && Root order >= quality order) if (! | | | if (! After && divisor = = = Read Mass set [maximum root] && infinite number of multiplication (after divisor, divisor) = = divisor) {
                            /* Top 2 Maximum limit squared operation */if (Prime document volume <= 1) read data ();
                            + + maximum prescribe; if (prime number file Volume > 1) IncreaseQuantity of quality (); Write change record (read mass number set [max.] + "\ r \ n" + max.
                        ToString (), "prime number", "Maximum root", divisor, false); } if (!) the minimum set of public times. Contains ("" + (!) Divisor: Divisor)) min. set + = "\ r \ n" + dividend + "" + (! Business?) Divisor: After divisor) + "" + (! Business?)
                        Quality order: root order);
                        if (output% 100 = = 0) write to change the record (minimum public times set, "prime number", "Minimum public multiplier set", divisor, false);
                        if (maximum root > 1) Root order = maximum radical;
                    Forward ();
                        } else {divisor = maximum radical > 4 reading mass set [+ + quality Order]: promotion (divisor); After divisor = maximum > 4?
                    Reading mass number Set [root order--]: after divisor;
                    } else {output + = "" + divisor;
                        if (+ + output% 100 = = 0) {Write change record (output, "prime number", "Mass set", "mass set" + divisor); Write Change records (dividend, "prime", "dividend", divisor, FALSE); output = "";
                        Output amount = 0; if (prime number file volume = = 0) {Write change record (read mass set [ten] + "\r\n10", "Prime", "Max Root", divisor, false)
                            ;
                        Read-in data ();
                    }} console.write ("{0}", divisor); if (prime number file volume = = 0) {Read mass set.
                        ADD (dividend);
                    Root order + +;
                    } else Root order = maximum root;
                Forward (); }} while (divisor. Length < Int.

 MaxValue);

Text Manipulation Reference

The previous answer to a small range of operations does not consider the optimization algorithm to improve the efficiency of the problem, today, the prime number is an int. MaxValue-bit string range to be calculated, such a large range has to be considered, optimization is necessary, from 4 points to optimize:
1. The divisor does not press 1 step, instead of 2 step, the prime number 2 is odd, such as 3 start by stepping 2 is odd, thus skipping even to reduce the amount of cyclic calculation;
2. The divisor does not press 1 steps, and from the found set of the quality of the collection, so that you can avoid the 2 3 5 7 11, such as a multiple, one of all the even number is a multiple of 2, and the second can be excluded by the number of natural is a multiple, multiples do not have to do redundant operations, but also not in prime Therefore, the use of prime numbers for the divisor is the most reasonable to reduce the cycle of calculation, quality has another feature is that in addition to 5 units of more than 5 of the quality no longer contain the number of bits 5;
3. The maximum multiplier of a number is its square root, in the mass set to find the order of the square root, and then push back one order, is to take off a prime number to do divisor, the divisor of the range arranged in the most reasonable interval to reduce the amount of cyclic calculation;
4. Two-way operation to improve the elimination of non-prime number of speed to reduce the cyclic calculation;
Through the above 4 optimization, significantly improve the computational speed, the previous 2 days of calculation, now easy to do. Whether optimization is to be analyzed.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.