Giter Club home page Giter Club logo

primes's Introduction

Primes

This application generates a list of prime numbers and then uses them to print a multiplication table.

It includes the following files:

  • prime_app.rb - Application entry point that creates a MultiplicationTablePrinter class with a trial division prime generator and then prints a multiplication table
  • multiplication_table_printer.rb - Class that uses a generator to make a number list and print a multiplication table
  • trial_division_prime_generator.rb - simple prime generator, good for small size lists
  • eratosthenes_prime_generator.rb - implementation of Erotosthenes prime sieve, uses more memory but better for larger lists
  • unit_test.rb - Minitest unit test for the MultiplicationTablePrinter class and prime generator classes
  • benchmark.rb - Minitest performance test for the two prime generators

Bonus: http://plnkr.co/edit/nFyzaTLgNirFGOCFsewq

How to run the app:

./prime_app.rb 

Run the unit tests:

ruby -Ilib:test unit_test.rb

Run the performance test:

ruby -Ilib:test benchmark.rb

Note: I'm using ruby 2.1.2p95

##Implementation Notes

Since the requirement is to print a table with the first 10 prime numbers, I used a trial division generator for the main app because although it has a complexity of O(N^2/2) it is sufficiently fast for the requirement and less complicated, thus easier to maintain and less likely to introduce bugs.

After performance testing the trial division generator with N = 10000 and experiencing the terrible performance, I decided to also implement a more efficient prime sieve. I decided on the Eratosthenes sieve because it is easy to understand and there are no requirements to make the fastest, most efficient prime generator. While this algorithm is faster, it requires more memory, so in cases where memory is limited, a different algorithm would be better.

At first I couldn’t think of a really effective way to unit test a prime generator, but then I thought that with two different implementations we can write tests that compare the two, which is cool.

I implemented the MultiplicationTablePrinter to use a generic generator class to create a list of numbers and then print the numbers. The two prime number generators can be used to create the number list, or any other class that provides a create_number_list method can also be used

I threw all of the unit tests in the same file just to make it easier to review. Each class should probably have it’s own unit test file in the real world. I also put all the files in the same directory to make reviewing easier, no need to navigate into test directories or whatnot.

##Style Notes

I've been writing more Java and JavaScript recently so that syntax is most present in my brain at this moment. I've done my best to use the typical Ruby style here. With less Java and More Ruby I can improve this, and of course I would also learn and use whatever style guidelines you all have.

I try to code with descriptive method and variables names to reduce the amount of comments needed. My philosophy is that it’s better to write more understandable code than it is to write long comments that could get out of sync with the code.

primes's People

Contributors

mer10z 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.