Skip to content

Instantly share code, notes, and snippets.

@gangster
Created February 19, 2015 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gangster/dac07850210251b9bd56 to your computer and use it in GitHub Desktop.
Save gangster/dac07850210251b9bd56 to your computer and use it in GitHub Desktop.
Primex Coding Challenge

Primer

Objective

Write a program that prints out a multiplication table of the first N prime numbers.

The program must run from the command line and print a single table to the screen.

Across the top and down the left side should be the N primes, and the body of the table should contain the product of multiplying these numbers.

Requirements

  • Consider Code complexity
  • Do Not use a library method for Prime (write your own)
  • Use Ruby
  • Write Specs

Bonus Requirements

  • SOLID principles
  • 100% test coverage
  • Simple & Readable
  • Idiomatic
  • Minimal 3rd party dependencies
  • Punt on time complexity optimizations

Examples

$ time bin/run.rb 10
+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+
|        Prime Number Multiplication Table (10 numbers)        |
+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+
|    | 2  | 3  | 5   | 7   | 11  | 13  | 17  | 19  | 23  | 29  |
+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+
| 2  | 4  | 6  | 10  | 14  | 22  | 26  | 34  | 38  | 46  | 58  |
+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+
| 3  | 6  | 9  | 15  | 21  | 33  | 39  | 51  | 57  | 69  | 87  |
+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+
| 5  | 10 | 15 | 25  | 35  | 55  | 65  | 85  | 95  | 115 | 145 |
+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+
| 7  | 14 | 21 | 35  | 49  | 77  | 91  | 119 | 133 | 161 | 203 |
+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+
| 11 | 22 | 33 | 55  | 77  | 121 | 143 | 187 | 209 | 253 | 319 |
+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+
| 13 | 26 | 39 | 65  | 91  | 143 | 169 | 221 | 247 | 299 | 377 |
+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+
| 17 | 34 | 51 | 85  | 119 | 187 | 221 | 289 | 323 | 391 | 493 |
+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+
| 19 | 38 | 57 | 95  | 133 | 209 | 247 | 323 | 361 | 437 | 551 |
+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+
| 23 | 46 | 69 | 115 | 161 | 253 | 299 | 391 | 437 | 529 | 667 |
+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+
| 29 | 58 | 87 | 145 | 203 | 319 | 377 | 493 | 551 | 667 | 841 |
+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+
bin/run.rb 10  0.04s user 0.01s system 95% cpu 0.051 total

$ time bin/run.rb 5
+-------+-------+-------+-------+-------+-------+
| Prime Number Multiplication Table (5 numbers) |
+-------+-------+-------+-------+-------+-------+
|       | 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   |
+-------+-------+-------+-------+-------+-------+
bin/run.rb 5  0.04s user 0.01s system 96% cpu 0.053 total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment