Java algorithm for solving the quality factor

Source: Internet
Author: User
Public classQualityfactor {/** * Because no matter how the number of primes can be computed by multiplication in 1 9, except that 1 and 2 only need to continue to be divisible by 2-9. * This argument uses the extraction of the minimum polynomials to compute the result One important thing to avoid, of course, is that when it's a single-digit number, it's 1, direct return at 2, 3, 5, 7 The advantage of this calculation is to avoid the traditional recursive calculation from 1 to n is more efficient to calculate the number of primes facing thousands of data use * Also avoid excessive use of this calculation Method (redundant redundancy calculation): * To determine the number of primes: 2 to sqrt (the square root of this number), if divisible, it indicates that this number is not prime, Vice is prime this algorithm is faster * Avoid redundant redundancy * * *Public BooleanIsprimenumber (intDivisorintNumber) {if(number% divisor = 0)Return false;else if(number = 1 | | | number = 2 | | number = 3 | | number = 5 | | number = 7 | | number = ONE | | | Number = = 17 | | Number = 19)Return true;else if(Number <= 20)Return false;else if(divisor = 9) { returnIsprimenumber (one, divisor); }else if(Divisor > 9) {if(Divisor < MATH.SQRT (number)) { returnIsprimenumber (divisor + 1, number); }else if(Divisor = = math.sqrt (number))Return false;else return True; } returnIsprimenumber (divisor + 1, number); }Public voidGetqualityfactor (intNumber) {String out = number +"=";if(Isprimenumber (2, number)) out = out + number;Else{ while(number!= 1) { for(intj = 2; J <= number; J + +) {/* If every number can be divisible by J let number/=j*/if(number% J = = 0)
                        {Number/= J; /* Divide and finish to determine whether it is prime so as to avoid the last remaining a relatively large prime number and then have to do the calculation of repeated calculation * *if(number!=1) {out = = j +"x";if(Isprimenumber (2,number))
                                {out = = number;
                            Number = 1; }
                        }Elseout + = j; Break; {}}} System. out. println (out); }
}

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.