Git Product home page Git Product logo

primes.py's Introduction

primes.py

Prime number library for Python 3

If you want to help develop, open an issue or fork the repo, make your changes and submit a pull request.

Build Status

Primes

A list-like object that automatically generates additional prime numbers as required. Supports membership testing and slicing for sequences of prime numbers

>>> primes = Primes()
>>> 10 in primes
False
>>> 11 in primes
True
>>> primes[10:20]
[31, 37, 41, 43, 47, 53, 59, 61, 67, 71]
>>> primes[15:10:-2]
[53, 43, 37]
>>> primes[100]
547
>>> primes.index(23)
8

There are also a number of functions for prime generation and primality testing:

primes_up_to

Implementation of Sieve of Eratosthenes

Returns all primes up to (and including) x

Can pass in primes to decrease execution time

>>> primes_up_to(10)
[2, 3, 5, 7]

>>> primes_up_to(20, [2, 3, 5, 7])
[2, 3, 5, 7, 11, 13, 17, 19]

is_prime

Returns True if x is a prime number, False if it is not

Can pass in known primes to decrease execution time

>>> is_prime(191)
True

>>> is_prime(192)
False

n_primes

Returns the first n primes

Can pass in known primes to decrease execution time

>>> n_primes(5)
[2, 3, 5, 7, 11]

>>> n_primes(10, [2, 3, 5, 7, 11])
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

nth_prime

Returns the nth prime (i.e. the 3rd prime, the 6th prime)

Can pass in known primes to decrease execution time

>>> nth_prime(1000)
7919

>>> nth_prime(4, [2, 3, 5, 7, 11])
7

composites_up_to

Returns all composite (non-prime greater than 1) numbers up to (and including) x

Can pass in known primes to decrease execution time

>>> composites_up_to(10)
[4, 6, 8, 9, 10]

>>> composites_up_to(20, [2, 3, 5, 7, 11, 13, 17, 19, 23])
[4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20]

next_prime

Given primes, returns the next prime

Uses method of trial division

This is much less efficient than primes_up_to for generating ranges of primes

>>> next_prime([2, 3, 5, 7, 11])
13

>>> next_prime(primesUpTo(1000000))
1000003

primes.py's People

Contributors

liam-m avatar smllmn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

primes.py's Issues

Only check primes of form 6n+1 and 6n-1

Currently the sieve_of_eratosthenes method has an optimisation: all primes other than 2 are a multiple of 2, so that only odd numbers must be checked. This gives roughly a 2x speed up and reduces memory usage by about half.

We can add a similar optimisation by also treating multiples of 3 as a special case: only numbers of form 6n+1 and 6n-1 have to be checked, as all other numbers are multiples of 2 or 3. See https://www.quora.com/Why-do-prime-numbers-always-satisfy-the-6n+1-and-6n-1-conditions-Is-there-any-mathematical-logic-behind-it. This should reduce execution time and memory usage by about a third.

Analyse live mutants

I added mutation testing on Travis in #9.

94/520 (18.1%) of the mutants survived. Analyse each one and determine whether it is equivalent or not.

If it is not equivalent, write a test to kill it.
If it is equivalent decide if the syntax of the program can be changed to prevent the mutant from being generated.

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.