Kotlin algorithm for getting started with a full number of algorithms

Source: Internet
Author: User
Tags square root
/* If a number is exactly equal to the sum of its factors, this number is called the "end number". */classCompletenumber {private VarFirstfactornumber: Int = 0/** * Because the non-prime number can be calculated by multiplying by 1 9 in addition to 1 and 2 only if it is possible to continue divisible by 2-9 * This statement uses the extraction of the minimum common divisor to calculate * Of course to avoid a The important question is that when it's a single digit, it's 1, 2, 3, 5, 7. The advantage of this calculation is to avoid the iterative calculation of the traditional recursion from 1 to n more efficient calculation of the number of tens of thousands of data using * Also avoids excessive use of this algorithm (redundant repeatability Calculation): * Method of judging primes: A number is removed from 2 to sqrt (the square root of this number), if it can be divisible, it indicates that the number is not a prime, the inverse is prime this algorithm faster * avoid duplication of redundancy * * FunIsprimenumber (Divisor:int, Number:int): Boolean {if(number% Divisor = = 0) {Firstfactornumber= Divisorreturn False }if(number = = 1 | | number = = 2 | | number = = 3 | | number = = 5 | | number = = 7 | | | Number = = 17 | | Number = = 19)return Trueelse if(Number <= 20) {Firstfactornumber= 1return False }else if(divisor = = 9) {returnIsprimenumber (one, divisor)}else if(Divisor > 9) {if(Divisor < Math.sqrt (number.todouble ())) {returnIsprimenumber (divisor + 1, number)}else if(divisor.todouble () = = Math.sqrt (number.todouble ())) {Firstfactornumber= Divisorreturn False }Elsereturn True }returnIsprimenumber (divisor + 1, number)}/* Call optimization algorithm */ FunIscompletenumber (number:int) {if(Number < 6) {println ("from 1 to"+ number +"No complete number")
        }Else{ for(Iinch6..number) {/* Prime cannot be a complete number so it can be computed directly skipping * each time the calculation of the number of primes is calculated if not by calculation * is the first factor +1 Calculate the starting point to avoid the start-up repetition calculation */if(Isprimenumber (2, number))Continuevarsum = 0if(Firstfactornumber> 1) Sum + = 1 +Firstfactornumber else if(Firstfactornumber= = 1) sum + = 1 for(jinchFirstfactornumber+ 1 until i) {if(i% j = 0) {sum = sum + j}}if(sum = = i)
        {println (i)}} }}/* Traditional practice */ FunTradition () {varS:int for(Iinch1..1000000) {s = 0 for(jinch1 until I)if(i% j = 0) s = s + jif(s = = i) print (i.tostring () +" ")} println ()}}

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.