Kotlin algorithm Entry calculation factorization __ algorithm

Source: Internet
Author: User
Tags square root
/* Each non-prime (composite) can be written as a number of primes (also known as prime numbers) in the form of multiplication, and these primes are called the composite's mass factor.
For example, 6 can be decomposed into 2x3, and 24 can be decomposed into 2x2x2x3. Now, your program reads an integer in a [2,100000] range, and then outputs its mass factorization, and when it reads a prime number, it outputs itself */classQualityfactor {/** * because the non-prime number can be calculated by multiplying by the 1 9 in addition to 1 and 2 only if it is possible to continue to be divisible by 2-9 * This statement uses the extraction of the minimum common divisor to calculate * Of course, to avoid an important problem is that when it is a single digit when it is 1, 2, 3, 5, 7 when the direct return * The advantage of this calculation is to avoid the traditional recursion from 1 to n the repeated calculation of more efficient calculation of the number of thousands above the use of data * Also avoids excessive use of this calculation Method (redundant repeatability calculation): * Method of Judging primes: 2 to sqrt (the square root of this number), if divisible, indicates that the number is not a prime, and the inverse is prime this algorithm is faster * avoids redundant redundancy */ FunIsprimenumber (Divisor:int, Number:int): Boolean {if(number% Divisor = = 0)return Falseelse if(number = = 1 | | number = = 2 | | number = = 3 | | number = = 5 | | number = = 7 | | | Number = = 17 | | Number = = 19)return Trueelse if(Number <= 20)return Falseelse if(divisor = = 9) {returnIsprimenumber (one, divisor)}else if(Divisor > 9) {return if(Divisor < Math.sqrt (number.todouble ())) {Isprimenumber (divisor + 1, number)}else if(divisor.todouble () = = Math.sqrt (number.todouble ()))falseElsetrue }returnIsprimenumber (divisor + 1, number)} FunGetqualityfactor (number:int) {varNumber = numbervarout = number.tostring () +"="  if(Isprimenumber (2, number)) out = out + numberElse{ while(number! = 1) { for(jinch2..number) {/* If every number can be divisible by J then let number/=j*/if(number% J = 0) {Number/= J//is divided by the number of primes to avoid the last large prime number and then the calculation of the repeat calculation */if(number! = 1) {out + = j.tostring () +"x" if(Isprimenumber (2, number))
                        {out + = number number = 1} }Else Out + = J Break }}}} 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.