Skip to content

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

@liam-m

Description

@liam-m

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions