Kotlin algorithm Getting started calculating primes and optimizing __ algorithms

Source: Internet
Author: User
Tags square root
classGetprimenumber {/* * gets [1,n] interval primes */ FunForeachnumbergetprime (number:int): list<int> {Valintegers = arraylist<int> () for(Iinch1 until number)if(Isprimenumber (2, I)) Integers.add (i)returnintegers}/** * Gets the prime number from the specified interval */ FunForeachnumbergetprimetospecifiedposition (Startposition:int, Endposition:int): list<int> {Valintegers = arraylist<int> () for(IinchStartPosition until Endposition)if(Isprimenumber (2, I)) Integers.add (i)returnintegers}/** * Print a prime number from 1 to n */ FunForeachprintnumbergetprime (number:int) { for(Iinch1 until number)if(Isprimenumber (2, I)) println ("number = [$I] is the prime number ")}/** * Print the prime number from the specified interval */ FunForeachprintnumbergetprimetospecifiedposition (Startposition:int, Endposition:int) { for(IinchStartPosition until Endposition)if(Isprimenumber (2, I)) println ("number = [$I] is the prime number ")}/** * Because the non-prime number can be calculated by multiplying by 1 9 in addition to 1 and 2 only if it is necessary to continue to be divisible by 2-9 * This statement uses the extraction of the minimum common divisor to calculate * Of course to avoid a heavy The problem is that when it is a single digit, it is 1, 2, 3, 5, 7, so the direct return of 20 of the prime number * The advantage of this calculation is to avoid the traditional recursion from 1 to n the repeated calculation of more efficient calculation of prime numbers facing more than thousands of data use * Also avoids excessive use of this Algorithm (redundant repeatability calculation): The method of judging the prime number: to remove 2 to sqrt (the square root of this number), if it can be divisible, it means that the number is not a prime, the inverse is prime, the algorithm is faster * Avoid 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)}}

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.