Git Product home page Git Product logo

sieve-of-atkin's Introduction

  • A. O. L. Atkin and D. J. Bernstein, "Prime Sieves Using Binary Quadratic Forms", in Mathematics of Computation, vol. 73, no. 246, pp. 1023โ€“1030.
  • D. J. Bernstein, primegen 0.97.

Sieve of Atkin

A fast prime generator. I have implemented the technique described by Atkin and Bernstein (as opposed to brute-forcing quadratic congruences). While Bernstein has already provided an optimised implementation, I found the code very hard to understand, and it does not store the list of primes, so I decided to write it myself.

Also included here is a wheel-factorised sieve of Eratosthenes implementation which proceeds mostly along the same lines as the sieve of Atkin.

style tests

Implementation Notes

Both sieves start generating primes from 7 onwards. The first three primes (i.e. 2, 3 and 5) must be handled separately.

Performance

The sieve of Atkin is faster than the sieve of Eratosthenes. Tabulated below are the times taken (to two significant figures) to construct the sieves up to n on my machine. Note that the time taken to iterate over the primes (after constructing the sieves) will, in theory, be identical for a given n because the two sieves store the list of primes in the same manner.

n Eratosthenes Atkin
216 0 ms 0 ms
217 0 ms 0 ms
218 1 ms 0 ms
219 2 ms 0 ms
220 4 ms 0 ms
221 7 ms 1 ms
222 16 ms 3 ms
223 32 ms 6 ms
224 69 ms 11 ms
225 140 ms 25 ms
226 300 ms 50 ms
227 680 ms 210 ms
228 1800 ms 560 ms
229 3900 ms 1400 ms
230 8700 ms 3000 ms

Related

I rewrote this sieve of Atkin implementation in Rust to augment my Project Euler solutions library.

sieve-of-atkin's People

Contributors

tfpf avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.