Prime Products
Prime Products is a command-line tool which outputs the multiplication table of the first n prime numbers.
So, for instance, where n=5, you'd get a result like this:
+----+----+----+----+----+-----+
| * | 2 | 3 | 5 | 7 | 11 |
| 2 | 4 | 6 | 10 | 14 | 22 |
| 3 | 6 | 9 | 15 | 21 | 33 |
| 5 | 10 | 15 | 25 | 35 | 55 |
| 7 | 14 | 21 | 35 | 49 | 77 |
| 11 | 22 | 33 | 55 | 77 | 121 |
+----+----+----+----+----+-----+
The first row and column of the table contains the first 5 primes. Each other cell contains the product of the primes for the corresponding row and column.
Installation
Install the gem by running gem install prime_products.
In development, you can install this gem by cloning the repository and running bundle.
Usage
To generate a table of prime products, run the prime_products executable, providing the size of the table as the first argument:
# If you have installed this gem from source, you will need to run
# `bundle exec prime_products 10`.
prime_products 10
If you do not provide an argument, we will use the first 5 by default.
Benchmarking
This gem uses a naive method of finding the first n primes which does not scale especially well. However, this seems reasonable given that the primes will be displayed on-screen, and so it is unlikely you will want to see a large number at once.
Despite being a naive solution, this is still moderately performant. On my machine:
- finding the first 10 takes ~0.00004s
- finding the first 100 takes ~0.001s
- finding the first 1000 takes ~0.2s
- finding the first 10000 takes ~31s
It is unlikely that you would want a table of more than 1000, which only takes ~0.2 seconds. If we wanted to use these numbers elsewhere, and needed more, then I would optimise this code.
You can see full benchmark results, comparing the Ruby standard library's implementation (Prime.first) to mine, by running ruby benchmarks/prime_numbers.rb.
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. Run rake lint to check the code style with Rubocop.
You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.