Git Product home page Git Product logo

prime-checker's Introduction

Prime checker

This is a simple command line program for checking whether a number is prime, or getting all primes within a range.

If the program gets a single argument, then it checks if the value is prime:

./primes 1000003
Is prime

If the program gets two arguments, then it prints all primes between the two values:

./primes 1001000 1001100
1001003 1001017 1001023 1001027 1001041 1001069 1001081 1001087 1001089 1001093

For compiling the program, just run make in the root directory of the project. If you are on Windows using MinGW, you might need to run mingw32-make instead.

Or you can just download the compiled binaries:

Algorithm

In order to check if a number is prime, I tool advantage of the following properties

  • The biggest prime factor of a positive integer, excluding itself, is smaller or equal than its square root.
  • With the exception of 2 and 3, all numbers that are prime can be written in the form of 6n ± 1 (with n being a positive integer).

Those properties allowed to greatly reduce the amount of divisors to try against the input.

In order to generate a range of prime numbes, I used the Sieve of Eratosthenes. The sieve initially contains all values in the range, which are tested sequentially. When a value is tested, its multiples are removed from the sieve and not tested (because they certainly cannot be primes).

prime-checker's People

Contributors

tbpaolini avatar

Watchers

 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.