sb_prime_table 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +19 -2
- data/bin/setup +0 -2
- data/exe/sb_prime_table +7 -2
- data/lib/sb_prime_table/version.rb +1 -1
- data/time_profile.txt +116 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a110255579fd41d8e245381aedc252789a51195
|
4
|
+
data.tar.gz: b54234b10d0af303c7a8e70689b7d7e8c0931a12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 606d05548e2f18a70ed0f9bc851beb0133470c42e256eb778e894fcaf131b660596a87cd8cbae0ced0e80e93aea3618ea1a4e67cf93d1cbf09382c406ced0598
|
7
|
+
data.tar.gz: 55bc97825c4b5613f85f8d3ab05e8290260fabff14af56546647ddbeb3829af64618af58942b8e3c72ddd29f527864617c7db779713dae1afc457008d349c5c0
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# SbPrimeTable
|
2
2
|
|
3
|
-
Code Challenge:
|
3
|
+
## Code Challenge:
|
4
4
|
|
5
5
|
This is a program that prints out a multiplication table of the first N prime numbers.
|
6
6
|
|
@@ -8,6 +8,16 @@ This is a program that prints out a multiplication table of the first N prime nu
|
|
8
8
|
- The first row and column of the table will have the N primes
|
9
9
|
- The user is allowed to specify different table sizes through command line options. In case of no argument passed into the program, the first 10 primes will be used by default
|
10
10
|
|
11
|
+
### Addressing Notes:
|
12
|
+
|
13
|
+
- In terms of complexity, the major complexity issue is creating the multiplication table. Each entry in the table requires an input so for each value extra value in `n`, there will be a need for an extra row and column. My initial approach was to use a dictionary that used the index as a reference to the multiplication. With that index I would be able to skip computing the values for the lower triangle in the table. The approach was not entirely better since it doesn't prevent the code from looping twice over the primes array and performing assignments at the very least. In other words, the method `#multiplication_table` has O(n^2) complexity.
|
14
|
+
|
15
|
+
- While not having used the Prime class from stdlib, I am acquainted with some of the techniques used (i.e. Monkey Patching). I normally prefer not to monkey patch classes as important as Integer as I did in `lib/primer:60`, but considering this is done by the Prime class, I decided to go ahead with the patch.
|
16
|
+
|
17
|
+
- In terms of running speed when called with `n=10`, `n=100`, and `n=1000`, we can see from [time_profile](time_profile.txt) that each step increases the amount of calls by 2 orders of magnitude when calling methods such as `#display_table` and `#multiplication_table`.
|
18
|
+
|
19
|
+
- The `#multiplication_table` and `#display_table` could have been joined into a single method that had the formatted output. I did however decided against this considering different formats could be selected by extracting the display table method into its own.
|
20
|
+
|
11
21
|
## Installation
|
12
22
|
|
13
23
|
Add this line to your application's Gemfile:
|
@@ -27,7 +37,14 @@ Or install it yourself as:
|
|
27
37
|
## Usage
|
28
38
|
|
29
39
|
Once the gem has been installed, you should be able to run the `sb_prime_table` command from the command line and get a prime multiplication table.
|
30
|
-
Running the command without arguments will result in a 10x10 table. Large values (i.e. above the order of 1000) will take long to compute. Stop the program by pressing `ctrl + c` in your keyboard
|
40
|
+
Running the command without arguments will result in a 10x10 table. Large values (i.e. above the order of 1000) will take long to compute. Stop the program by pressing `ctrl + c` in your keyboard.
|
41
|
+
|
42
|
+
## Dependencies
|
43
|
+
|
44
|
+
To run tests and modify the existing code you must have Bundler installed. To install in case of not having it run `gem install bundler` from your command line.
|
45
|
+
Once the installation is complete, clone/check out this repo into your local machine and follow the steps in the following section.
|
46
|
+
|
47
|
+
Gem dependencies will be installed as part of the following steps.
|
31
48
|
|
32
49
|
## Development
|
33
50
|
|
data/bin/setup
CHANGED
data/exe/sb_prime_table
CHANGED
@@ -4,8 +4,13 @@ require 'sb_prime_table'
|
|
4
4
|
if ARGV.empty?
|
5
5
|
SbPrimeTable::create_table
|
6
6
|
elsif (Integer ARGV[0] rescue false)
|
7
|
-
|
7
|
+
n = ARGV[0].to_i
|
8
|
+
|
9
|
+
if n > 0
|
10
|
+
SbPrimeTable::create_table ARGV[0].to_i
|
11
|
+
else
|
12
|
+
puts "Negative values are not acceptable"
|
8
13
|
else
|
9
|
-
puts "Acceptable arguments include
|
14
|
+
puts "Acceptable arguments include a positive Integer or no argument"
|
10
15
|
puts "In the case of no argument, 'n' will be 10"
|
11
16
|
end
|
data/time_profile.txt
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
n = 10
|
2
|
+
|
3
|
+
% cumulative self self total
|
4
|
+
time seconds seconds calls ms/call ms/call name
|
5
|
+
0.00 0.00 0.00 1 0.00 0.00 TracePoint#enable
|
6
|
+
0.00 0.00 0.00 1 0.00 0.00 Class#inherited
|
7
|
+
0.00 0.00 0.00 5 0.00 0.00 Module#method_added
|
8
|
+
0.00 0.00 0.00 1 0.00 0.00 Module#private
|
9
|
+
0.00 0.00 0.00 1 0.00 0.00 Kernel#respond_to?
|
10
|
+
0.00 0.00 0.00 1 0.00 0.00 Integer#integer?
|
11
|
+
0.00 0.00 0.00 112 0.00 0.00 Integer#<=>
|
12
|
+
0.00 0.00 0.00 73 0.00 0.00 Integer#prime?
|
13
|
+
0.00 0.00 0.00 30 0.00 0.00 Primer#get_primes
|
14
|
+
0.00 0.00 0.00 54 0.00 0.00 Integer#succ
|
15
|
+
0.00 0.00 0.00 28 0.00 0.00 Math.sqrt
|
16
|
+
0.00 0.00 0.00 29 0.00 0.00 Range#each
|
17
|
+
0.00 0.00 0.00 28 0.00 0.00 Enumerable#none?
|
18
|
+
0.00 0.00 0.00 153 0.00 0.00 Array#push
|
19
|
+
0.00 0.00 0.00 1 0.00 0.00 Array#flatten
|
20
|
+
0.00 0.00 0.00 133 0.00 0.00 Primer#multiplication_table
|
21
|
+
0.00 0.00 0.00 12 0.00 0.00 Array#each
|
22
|
+
0.00 0.00 0.00 1 0.00 0.00 Primer#initialize
|
23
|
+
0.00 0.00 0.00 1 0.00 0.00 Class#new
|
24
|
+
0.00 0.00 0.00 2 0.00 0.00 Array#last
|
25
|
+
0.00 0.00 0.00 121 0.00 0.00 Integer#to_s
|
26
|
+
0.00 0.00 0.00 1 0.00 0.00 String#to_s
|
27
|
+
0.00 0.00 0.00 121 0.00 0.00 String#rjust
|
28
|
+
0.00 0.00 0.00 133 0.00 0.00 Primer#display_table
|
29
|
+
0.00 0.00 0.00 12 0.00 0.00 Array#map
|
30
|
+
0.00 0.00 0.00 11 0.00 0.00 Array#join
|
31
|
+
0.00 0.00 0.00 12 0.00 0.00 IO#write
|
32
|
+
0.00 0.00 0.00 2 0.00 0.00 IO#puts
|
33
|
+
0.00 0.00 0.00 2 0.00 0.00 Kernel#puts
|
34
|
+
0.00 0.00 0.00 1 0.00 0.00 TracePoint#disable
|
35
|
+
0.00 0.01 0.00 1 0.00 10.00 #toplevel
|
36
|
+
|
37
|
+
|
38
|
+
--------
|
39
|
+
|
40
|
+
n = 100
|
41
|
+
|
42
|
+
% cumulative self self total
|
43
|
+
time seconds seconds calls ms/call ms/call name
|
44
|
+
26.09 0.06 0.06 10303 0.01 0.03 Primer#display_table
|
45
|
+
17.39 0.10 0.04 10303 0.00 0.02 Primer#multiplication_table
|
46
|
+
13.04 0.13 0.03 541 0.06 0.11 Range#each
|
47
|
+
13.04 0.16 0.03 102 0.29 2.25 Array#map
|
48
|
+
8.70 0.18 0.02 102 0.20 1.47 Array#each
|
49
|
+
8.70 0.20 0.02 10503 0.00 0.00 Array#push
|
50
|
+
8.70 0.22 0.02 10201 0.00 0.00 String#rjust
|
51
|
+
4.35 0.23 0.01 10201 0.00 0.00 Integer#to_s
|
52
|
+
0.00 0.23 0.00 1 0.00 0.00 TracePoint#enable
|
53
|
+
0.00 0.23 0.00 1 0.00 0.00 Class#inherited
|
54
|
+
0.00 0.23 0.00 5 0.00 0.00 Module#method_added
|
55
|
+
0.00 0.23 0.00 1 0.00 0.00 Module#private
|
56
|
+
0.00 0.23 0.00 1 0.00 0.00 Kernel#respond_to?
|
57
|
+
0.00 0.23 0.00 1 0.00 0.00 Integer#integer?
|
58
|
+
0.00 0.23 0.00 3519 0.00 0.00 Integer#<=>
|
59
|
+
0.00 0.23 0.00 2878 0.00 0.01 Integer#prime?
|
60
|
+
0.00 0.23 0.00 542 0.00 0.11 Primer#get_primes
|
61
|
+
0.00 0.23 0.00 2437 0.00 0.00 Integer#succ
|
62
|
+
0.00 0.23 0.00 540 0.00 0.00 Math.sqrt
|
63
|
+
0.00 0.23 0.00 540 0.00 0.06 Enumerable#none?
|
64
|
+
0.00 0.23 0.00 1 0.00 0.00 Array#flatten
|
65
|
+
0.00 0.23 0.00 1 0.00 110.00 Primer#initialize
|
66
|
+
0.00 0.23 0.00 1 0.00 110.00 Class#new
|
67
|
+
0.00 0.23 0.00 2 0.00 0.00 Array#last
|
68
|
+
0.00 0.23 0.00 1 0.00 0.00 String#to_s
|
69
|
+
0.00 0.23 0.00 101 0.00 0.00 Array#join
|
70
|
+
0.00 0.23 0.00 102 0.00 0.00 IO#write
|
71
|
+
0.00 0.23 0.00 2 0.00 0.00 IO#puts
|
72
|
+
0.00 0.23 0.00 2 0.00 0.00 Kernel#puts
|
73
|
+
0.00 0.23 0.00 1 0.00 0.00 TracePoint#disable
|
74
|
+
0.00 0.23 0.00 1 0.00 230.00 #toplevel
|
75
|
+
|
76
|
+
--------
|
77
|
+
|
78
|
+
n = 1000
|
79
|
+
|
80
|
+
% cumulative self self total
|
81
|
+
time seconds seconds calls ms/call ms/call name
|
82
|
+
32.89 6.71 6.71 1003003 0.01 0.03 Primer#display_table
|
83
|
+
17.89 10.36 3.65 1003003 0.00 0.02 Primer#multiplication_table
|
84
|
+
14.85 13.39 3.03 1002 3.02 23.61 Array#map
|
85
|
+
14.71 16.39 3.00 1002 2.99 14.65 Array#each
|
86
|
+
5.34 17.48 1.09 1002001 0.00 0.00 String#rjust
|
87
|
+
4.85 18.47 0.99 1002001 0.00 0.00 Integer#to_s
|
88
|
+
4.41 19.37 0.90 7919 0.11 0.28 Range#each
|
89
|
+
3.43 20.07 0.70 1005003 0.00 0.00 Array#push
|
90
|
+
0.49 20.17 0.10 93543 0.00 0.01 Integer#prime?
|
91
|
+
0.39 20.25 0.08 102462 0.00 0.00 Integer#<=>
|
92
|
+
0.34 20.32 0.07 86624 0.00 0.00 Integer#succ
|
93
|
+
0.20 20.36 0.04 1001 0.04 0.04 Array#join
|
94
|
+
0.10 20.38 0.02 7920 0.00 0.29 Primer#get_primes
|
95
|
+
0.05 20.39 0.01 7918 0.00 0.13 Enumerable#none?
|
96
|
+
0.05 20.40 0.01 1002 0.01 0.01 IO#write
|
97
|
+
0.00 20.40 0.00 1 0.00 0.00 TracePoint#enable
|
98
|
+
0.00 20.40 0.00 1 0.00 0.00 Class#inherited
|
99
|
+
0.00 20.40 0.00 5 0.00 0.00 Module#method_added
|
100
|
+
0.00 20.40 0.00 1 0.00 0.00 Module#private
|
101
|
+
0.00 20.40 0.00 1 0.00 0.00 Kernel#respond_to?
|
102
|
+
0.00 20.40 0.00 1 0.00 0.00 Integer#integer?
|
103
|
+
0.00 20.40 0.00 7918 0.00 0.00 Math.sqrt
|
104
|
+
0.00 20.40 0.00 1 0.00 0.00 Array#flatten
|
105
|
+
0.00 20.40 0.00 1 0.00 8530.00 Primer#initialize
|
106
|
+
0.00 20.40 0.00 1 0.00 8530.00 Class#new
|
107
|
+
0.00 20.40 0.00 2 0.00 0.00 Array#last
|
108
|
+
0.00 20.40 0.00 1 0.00 0.00 String#to_s
|
109
|
+
0.00 20.40 0.00 2 0.00 5.00 IO#puts
|
110
|
+
0.00 20.40 0.00 2 0.00 5.00 Kernel#puts
|
111
|
+
0.00 20.40 0.00 1 0.00 0.00 TracePoint#disable
|
112
|
+
0.00 20.40 0.00 1 0.00 20400.00 #toplevel
|
113
|
+
|
114
|
+
|
115
|
+
--------
|
116
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sb_prime_table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Santiago Baus
|
@@ -90,6 +90,7 @@ files:
|
|
90
90
|
- lib/sb_prime_table/primer.rb
|
91
91
|
- lib/sb_prime_table/version.rb
|
92
92
|
- sb_prime_table.gemspec
|
93
|
+
- time_profile.txt
|
93
94
|
- vendor/bundle/ruby/2.4.0/bin/coderay
|
94
95
|
- vendor/bundle/ruby/2.4.0/bin/htmldiff
|
95
96
|
- vendor/bundle/ruby/2.4.0/bin/ldiff
|