multiply_prime_numbers_table 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1506319dd7940920c7efaaa4fd8b4d9b21ea91ce
4
+ data.tar.gz: 4ec6e307f93bad104f49d652145aecddcca584de
5
+ SHA512:
6
+ metadata.gz: 7ef6f638fb3d15369d8a3ec48ecaa8f7d2d01669ee4a4966ca773e08d431619f855fd12d8748b5df593eed44ac01c8a3cf568a129af859b7d921e0e9ed00f981
7
+ data.tar.gz: f70322c94cbf0d7190bd334ee6cfa0ea2231f7a1233e6cb8aad4ccdc17db6338077d3d6bb18971cea89c3ef176e5db930865bb45ac4b9321fe5269f4694f176b
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem 'rake'
4
+ gem 'rspec'
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.3)
5
+ rake (12.3.1)
6
+ rspec (3.7.0)
7
+ rspec-core (~> 3.7.0)
8
+ rspec-expectations (~> 3.7.0)
9
+ rspec-mocks (~> 3.7.0)
10
+ rspec-core (3.7.1)
11
+ rspec-support (~> 3.7.0)
12
+ rspec-expectations (3.7.0)
13
+ diff-lcs (>= 1.2.0, < 2.0)
14
+ rspec-support (~> 3.7.0)
15
+ rspec-mocks (3.7.0)
16
+ diff-lcs (>= 1.2.0, < 2.0)
17
+ rspec-support (~> 3.7.0)
18
+ rspec-support (3.7.1)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ rake
25
+ rspec
26
+
27
+ BUNDLED WITH
28
+ 1.16.1
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Multiply prime numbers table
2
+
3
+ This program prints out a multiplication table of the first N prime numbers.
4
+
5
+ ## Install
6
+
7
+ From terminal inside root project run:
8
+
9
+ ````
10
+ gem build multiply_prime_numbers_table.gemspec
11
+ gem install ./multiply_prime_numbers_table-1.0.0.gem
12
+ ````
13
+
14
+ Or install directly from ruby gems:
15
+
16
+ ````
17
+ gem install multiply_prime_numbers_table
18
+ ````
19
+
20
+ Install the dependencies with:
21
+
22
+ ````
23
+ bundle install
24
+ ````
25
+
26
+ ## Run
27
+
28
+ To run the program:
29
+
30
+ ````
31
+ ruby -Ilib ./bin/multiply_prime_numbers_table
32
+ ````
33
+
34
+ ## Description
35
+
36
+ * The program runs from the command line and print a table to STDOUT.
37
+ * The first row and column of the table have the N primes, with each cell containing the product of the primes for the corresponding row and column.
38
+ * Allows the user to specify different table sizes through a command line option. If the option is not used, the table will contain the first 10 primes by default.
39
+
40
+ ## Test
41
+
42
+ You can run tests with:
43
+
44
+ ````
45
+ rake
46
+ ````
47
+
48
+ Or
49
+
50
+ ````
51
+ rspec
52
+ ````
53
+
54
+ ## Comments
55
+
56
+ The complexity of the program is exponential, the bigger N, the more complex the operations will be. To illustrate this matter i add a benchmark report to measure the performance in get_primes method, since it is the method with the highest cyclomatic complexity.
57
+
58
+ To improve performance, the #get_primes method avoid to check the number if it's negative is 1 or is EVEN distinct of 2. With this considerations allow us to speed up the code and achieve high performance.
59
+
60
+ Even for this little example having everything in one file doesn’t scale well. Thats why we separate the logic related to prime number and prime array in distinct files.
61
+
62
+ Also decided to make small methods like #get_cell_size. Each method should make only one task, to simplify the code and allow it to scale. This method returns a number formatted in string, it's very simple but i decided to make it a method because this way is easier to modify. Maybe tomorrow we improve the way we print the table, and need to calculate cell size differently.
63
+
64
+ Talking about the way it prints the table, i think it can be improve. Maybe give the user some range in between where you ensure that the table will look good or a use gem, but i wanted to use my own code and for this example i think fulfills its mission.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ rescue LoadError
9
+ abort '### Please run bundle install ###'
10
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'multiply_prime_numbers_table'
4
+
5
+ MultiplyPrimeNumbersTable.start
@@ -0,0 +1,92 @@
1
+ class MultiplyPrimeNumbersTable
2
+ require_relative "multiply_prime_numbers_table/prime_array"
3
+
4
+ class << self
5
+ def start
6
+ entry = ""
7
+
8
+ # The program will keep running until users enters an X to Exit the program.
9
+ until entry == "X"
10
+ puts "\nMultiply prime numbers table"
11
+ print "=============================\n\n"
12
+ puts "Enter an integer N greater than zero to specify the table size."
13
+ puts "Any other entry will display a table containing the first 10 primes by default."
14
+ puts "If N is greater than 100000 first will calcualte the first N primes and then will confirm is user want to print the table."
15
+ puts "keep in mind that display a good table figure it depends on your screen size.\n\n"
16
+ integer = gets.strip.to_i
17
+ # If user enters something different of an integer greater than zero , the size table will be 10x10 by default.
18
+ size = integer != 0 ? integer : 10
19
+ if size > 100000
20
+ puts "This may take a while ..."
21
+ prime_num = PrimeArray.get_primes(size)
22
+ puts "Warning! Print this size of table may cause trouble, but if you want to print it press Y"
23
+ entry = gets.strip.upcase
24
+ entry == "Y" ? create_table(prime_num) : false
25
+ else
26
+ # Get array of primes with the size defined
27
+ prime_num = PrimeArray.get_primes(size)
28
+ # Print the table
29
+ create_table(prime_num)
30
+ end
31
+
32
+ puts "Press X to Exit or any other key to calculate another table"
33
+ entry = gets.strip.upcase
34
+ end
35
+
36
+ puts "Exit"
37
+ end
38
+
39
+ def create_table(prime_num)
40
+ # Define prime numbers for first row and column
41
+ first_row = first_column = prime_num
42
+
43
+ # Get longest num in array
44
+ longest_num = longest_num_in_array(first_row)
45
+
46
+ # Define size for cells in table
47
+ cell_size = get_cell_size(longest_num)
48
+
49
+ # Leave blank first cell position to match first row with second coulum
50
+ (longest_num+1).times do
51
+ print " "
52
+ end
53
+
54
+ # Print first row of table
55
+ first_row.each { |row_num|
56
+ # Each cell will leave blank a space before and after the number inside, as left and rigth padding
57
+ print (" "+cell_size+" ") % row_num
58
+ }
59
+
60
+ # Print 2 empty rows
61
+ print "\n\n"
62
+
63
+ first_column.each { |column_num|
64
+ # Print first colum of table
65
+ # After first number of each row leave blank a space
66
+ print (cell_size+" ") % column_num
67
+
68
+ # Print inside cells
69
+ first_row.each {|row_num|
70
+ # Calculate the product of the primes for the corresponding row and column that intersecting in this cell.
71
+ # Print the product number leaving blank a space before and after
72
+ print (" "+cell_size+" ") % (row_num * column_num)
73
+ }
74
+
75
+ # Print 2 empty rows
76
+ print "\n\n"
77
+ }
78
+ end
79
+
80
+ def longest_num_in_array(array)
81
+ # Get length of the larger number in last cell of table
82
+ length_larger_num = (array.last**2).to_s.size
83
+ end
84
+
85
+ def get_cell_size(num)
86
+ # With %-4d format you make sure to print every cell with same size even if a number have less digits
87
+ "%-" + (num).to_s + "d"
88
+ end
89
+
90
+ end
91
+
92
+ end
@@ -0,0 +1,15 @@
1
+ class Integer
2
+
3
+ def is_prime?
4
+ # Avoid to check number if is negative, is 1 or is EVEN distinct of 2
5
+ return false if self <= 1 || (self.even? && self > 2)
6
+
7
+ # returns false if the module dividing num by other number from 2 to the square root of itsel is zero
8
+ 2.upto(Math.sqrt(self).to_i) do |integer|
9
+ return false if (self % integer).zero?
10
+ end
11
+ # if none of the previous checks match it means it's a prime number
12
+ true
13
+ end
14
+
15
+ end
@@ -0,0 +1,30 @@
1
+ class PrimeArray
2
+ require_relative "prime"
3
+ require 'benchmark'
4
+
5
+ def self.get_primes(num)
6
+ time = Benchmark.measure {
7
+ @primes_array = []
8
+ counter = 2
9
+
10
+ until @primes_array.size == num
11
+ @primes_array << counter if counter.is_prime?
12
+ # Avoid to check even numbers because only even prime number is 2
13
+ (counter+1).even? ? counter += 1 : counter +=1
14
+ end
15
+ }
16
+
17
+ # I add this report just to show illustrate how it gets exponentialy complex as it increases num
18
+ puts "Time to takes to calculate first "+num.to_s+" prime numbers: "
19
+ n = time.real
20
+ Benchmark.bm(7) do |x|
21
+ x.report("for:") { for i in 1..n; a = "1"; end }
22
+ x.report("times:") { (n.to_i).times do ; a = "1"; end }
23
+ x.report("upto:") { 1.upto(n) do ; a = "1"; end }
24
+ end
25
+ puts "\n"
26
+
27
+ @primes_array
28
+ end
29
+
30
+ end
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require './lib/multiply_prime_numbers_table'
4
+
5
+ describe MultiplyPrimeNumbersTable do
6
+
7
+ before do
8
+ @table = MultiplyPrimeNumbersTable
9
+ end
10
+
11
+ describe "validations" do
12
+
13
+ context '#longest_num_in_array' do
14
+ it "Get length of the square of last number in array" do
15
+ array = [1,5,7,13]
16
+ expect(@table.longest_num_in_array(array)).to eql(3)
17
+ end
18
+ end
19
+
20
+ context '#get_cell_size' do
21
+ it "returns %Nd format" do
22
+ expect(@table.get_cell_size(5)).to eql("%-5d")
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require './lib/multiply_prime_numbers_table/prime_array'
4
+ require './lib/multiply_prime_numbers_table/prime'
5
+
6
+ describe PrimeArray do
7
+
8
+ describe "validations" do
9
+
10
+ context '#get_primes' do
11
+ it "returns an array" do
12
+ expect(PrimeArray.get_primes(10).class).to eql(Array)
13
+ end
14
+
15
+ it "returns an array with the size of the number that is passed as a parameter" do
16
+ expect(PrimeArray.get_primes(10).size).to eql(10)
17
+ end
18
+
19
+ it "returns an array of primes with the size of the number that is passed as a parameter" do
20
+ array = PrimeArray.get_primes(10).map{|num| num.is_prime?}
21
+ expect(array.include?(false)).to eql(false)
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require './lib/multiply_prime_numbers_table/prime'
4
+
5
+ describe Integer do
6
+
7
+ describe "validations" do
8
+
9
+ context '#is_prime?' do
10
+ it "Evaluating negative nummber -1" do
11
+ expect(-1.is_prime?).to eql(false)
12
+ end
13
+
14
+ it "Evaluating number 1" do
15
+ expect(1.is_prime?).to eql(false)
16
+ end
17
+
18
+ it "Evaluating number 2" do
19
+ expect(2.is_prime?).to eql(true)
20
+ end
21
+
22
+ it "Evaluating number 10" do
23
+ expect(10.is_prime?).to eql(false)
24
+ end
25
+
26
+ end
27
+ end
28
+
29
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multiply_prime_numbers_table
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Manuel Figueroa Andrade
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: The program run from the command line and print a table to STDOUT. The
14
+ first row and column of the table have the N primes, with each cell containing the
15
+ product of the primes for the corresponding row and column. Allow the user to specify
16
+ different table sizes through a command line option. If the option is not used,
17
+ the table will contain the first 10 primes by default.
18
+ email: m.figand@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - README.md
26
+ - Rakefile
27
+ - bin/multiply_prime_numbers_table
28
+ - lib/multiply_prime_numbers_table.rb
29
+ - lib/multiply_prime_numbers_table/prime.rb
30
+ - lib/multiply_prime_numbers_table/prime_array.rb
31
+ - spec/multiply_prime_numbers_table_spec.rb
32
+ - spec/prime_array_spec.rb
33
+ - spec/prime_spec.rb
34
+ homepage: http://rubygems.org/gems/multiply_prime_numbers_table
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.4.5.1
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Prints out a multiplication table of the first N prime numbers.
58
+ test_files: []