Sieve of Atkin. In mathematics, the sieve of Atkin is a fast, modern algorithm for finding all prime numbers up to a specified integer. It is an optimized version of the ancient sieve of Eratosthenes which does some preliminary work and then marks off multiples of the square of each prime, rather than multiples of the prime itself. It was created in 2004 by A. O. L. Atkin and Daniel J. Bernstein.[1] Algorithm[edit] In the algorithm: Pseudocode[edit] The following is pseudocode for a straightforward version of the algorithm: This pseudocode is written for clarity.
Explanation[edit] The algorithm completely ignores any numbers divisible by two, three, or five. All numbers n with modulo-sixty remainder 1, 13, 17, 29, 37, 41, 49, or 53 have a modulo-four remainder of 1. All numbers n with modulo-sixty remainder 7, 19, 31, or 43 have a modulo-six remainder of 1. All numbers n with modulo-sixty remainder 11, 23, 47, or 59 have a modulo-twelve remainder of 11.
Computational complexity[edit] See also[edit] Priority Queue (Haskell) Please note: this code is not a complete implementation as it is intended to act as a component to another article. As it is worked on, it will be improved, but at this time do not assume this is complete and general-purpose. A priority search queue is a queue structure that attaches a "priority" to a value. We can treat the "priority" as a key and the whole priority queue as an ordered mapping.
It is an extremely useful data type used in a wide variety of circumstances (and, indeed, the motive for this article is to provide infrastructure for an efficient Sieve of Eratosthenes implementation). Before we can start to code the functions, we should put some thought into how to represent the data structure. A PriorityQueue is a recursive data structure built as a tree. Note that only the key is limited to being a member of the Ord class.
The operations on a normal priority queue are insert, remove and often inspect. It is, in effect a mere alias for the Nil type constructor. Sadly, Ms. The Genuine Sieve of Eratosthenes. Stay on Target: Looking at Streams in Scala. Scala offers a a few powerful abstractions for working with sequences of values. Functional programming style encourages moving from mutable variables and state changes to declarative lists of all known states. Higher order functions like map, flatMap, and filter can very succinctly represent complex requirements. For example, suppose you were writing a search engine and a user asked for the 10th document about Scala in a database of 10,0000 HTML documents. In a functional style, you might declare the list of all documents, compute the score for each one, and then pick the 10th item. Documents.filter(isAboutScala)(9) The problem is that if you've got 10,000 big HTML documents, you'll need to first ask each one of 10,000 if they're about Scala to build the filtered list -- only to take the 10th document.
One solution is to use Scala's Stream construct; which is like a List which lazily evaluates the next item only when needed. So how do Streams work? Syntactic sugar and the apply() method. Van Emde Boas tree. A Van Emde Boas tree (or Van Emde Boas priority queue; Dutch pronunciation: [vɑn 'ɛmdə 'boɑs]), also known as a vEB tree, is a tree data structure which implements an associative array with m-bit integer keys. It performs all operations in O(log m) time. Notice that m is the size of the keys — therefore O(log m) is O(log log n) in a tree where every key below n is set, exponentially better than a full self-balancing binary search tree.
The vEB tree also has good space efficiency when it contains a large number of elements, as discussed below. It was invented by a team led by Peter van Emde Boas in 1975.[1] Supported operations[edit] A vEB supports the operations of an ordered associative array, which includes the usual associative array operations along with two more order operations, FindNext and FindPrevious:[2] How it works[edit] An example Van Emde Boas tree with dimension 5 and the root's aux structure after 1, 2, 3, 5, 8 and 10 have been inserted. .
FindNext[edit] ) lo = x % In code: .