prime_table 0.0.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.
- data/.gitignore +17 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +22 -0
- data/README.md +30 -0
- data/Rakefile +1 -0
- data/bin/prime_table +4 -0
- data/features/prime_table.feature +12 -0
- data/features/step_definitions/prime_table_definitions.rb +9 -0
- data/features/support/setup.rb +2 -0
- data/lib/prime_table.rb +6 -0
- data/lib/prime_table/cli.rb +17 -0
- data/lib/prime_table/multiplication_table.rb +38 -0
- data/lib/prime_table/prime.rb +46 -0
- data/lib/prime_table/version.rb +3 -0
- data/prime_table.gemspec +19 -0
- data/spec/multiplication_table_spec.rb +59 -0
- data/spec/prime_spec.rb +58 -0
- metadata +68 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Fernando Diaz
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# PrimeTable
|
2
|
+
|
3
|
+
Prints a multiplication table of prime numbers
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'prime_table'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install prime_table
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
prime_table # will print table for 10 primes
|
22
|
+
prime_table --amount=AMOUNT # will print table for AMOUNT primes
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
|
26
|
+
1. Fork it
|
27
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
28
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
29
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
30
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/prime_table
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Feature: PrimeTable
|
2
|
+
In order to know what is the values of multiplying different prime numbers
|
3
|
+
As a gem user
|
4
|
+
I want to see a multiplication table for prime numbers
|
5
|
+
|
6
|
+
Scenario: 10 numbers by default
|
7
|
+
When I run `prime_table`
|
8
|
+
Then I should be presented a prime multiplication table of 10 numbers
|
9
|
+
|
10
|
+
Scenario: custom prime numbers
|
11
|
+
When I run `prime_table --amount=13`
|
12
|
+
Then I should be presented a prime multiplication table of 13 numbers
|
@@ -0,0 +1,9 @@
|
|
1
|
+
Then(/^I should be presented a prime multiplication table of (\d+) numbers$/) do |number|
|
2
|
+
numbers = Integer.n_first_primes(number.to_i)
|
3
|
+
numbers.each { |number| assert_partial_output(number.to_s, all_output) }
|
4
|
+
|
5
|
+
values = numbers.product(numbers).each_slice(numbers.length)
|
6
|
+
values = values.map { |a| [a.first.first] + a.map {|i,j| i * j }}
|
7
|
+
|
8
|
+
values.each { |value| assert_partial_output(number.to_s, all_output) }
|
9
|
+
end
|
data/lib/prime_table.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'prime_table'
|
3
|
+
|
4
|
+
module PrimeTable
|
5
|
+
class CLI < Thor
|
6
|
+
desc "print AMOUNT", "Prints the multiplication table of the first AMOUNT primes"
|
7
|
+
method_option :amount, :type => :numeric, :default => 10
|
8
|
+
def print
|
9
|
+
numbers = Integer.n_first_primes(options[:amount])
|
10
|
+
|
11
|
+
table = PrimeTable::MultiplicationTable.new(numbers)
|
12
|
+
table.print
|
13
|
+
end
|
14
|
+
|
15
|
+
default_task :print
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'terminal-table'
|
2
|
+
|
3
|
+
module PrimeTable
|
4
|
+
class MultiplicationTable
|
5
|
+
PRINTER = Terminal::Table
|
6
|
+
|
7
|
+
def initialize(numbers)
|
8
|
+
@numbers = numbers
|
9
|
+
end
|
10
|
+
|
11
|
+
def print
|
12
|
+
puts PRINTER.new(:rows => matrix)
|
13
|
+
end
|
14
|
+
|
15
|
+
def matrix
|
16
|
+
@matrix ||= generate_matrix
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def generate_matrix
|
22
|
+
# we get the cardinal product of the primes number with itself
|
23
|
+
product = @numbers.product(@numbers)
|
24
|
+
|
25
|
+
# slice the product
|
26
|
+
values = product.each_slice(@numbers.length)
|
27
|
+
|
28
|
+
# calculate the multiplication of each pair
|
29
|
+
values = values.map { |a| [a.first.first] + a.map {|i,j| i * j }}
|
30
|
+
|
31
|
+
# adding the label row
|
32
|
+
values.unshift(['X'] + @numbers)
|
33
|
+
|
34
|
+
@matrix = values
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module PrimeTable
|
2
|
+
module Prime
|
3
|
+
def is_prime?
|
4
|
+
return false if self < 2
|
5
|
+
|
6
|
+
sqrt = Math.sqrt(self).to_i
|
7
|
+
(2..sqrt).none? { |i| self % i == 0}
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def n_first_primes(n = 10)
|
13
|
+
primes = []
|
14
|
+
number = 0
|
15
|
+
|
16
|
+
while (primes.length < n) do
|
17
|
+
number += 1
|
18
|
+
primes << number if number.is_prime?
|
19
|
+
end
|
20
|
+
primes
|
21
|
+
end
|
22
|
+
|
23
|
+
def method_missing(sym, *args)
|
24
|
+
if sym.to_s =~ /^first_(\d+)_primes$/
|
25
|
+
n_first_primes($1.to_i)
|
26
|
+
else
|
27
|
+
super
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def respond_to?(method, priv=false)
|
32
|
+
(method.to_s =~ /^first_(\d+)_primes$/) || super
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.included(base)
|
39
|
+
base.extend(ClassMethods)
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class Integer ; include(PrimeTable::Prime) end
|
data/prime_table.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'prime_table/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "prime_table"
|
8
|
+
gem.version = PrimeTable::VERSION
|
9
|
+
gem.authors = ["Fernando Diaz"]
|
10
|
+
gem.email = ["fdiazgarrido@gmail.com"]
|
11
|
+
gem.description = %q{Prints a multiplication table of prime numbers}
|
12
|
+
gem.summary = %q{Prints a multiplication table of prime numbers}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'prime_table'
|
2
|
+
|
3
|
+
describe PrimeTable::MultiplicationTable do
|
4
|
+
def table(numbers)
|
5
|
+
PrimeTable::MultiplicationTable.new(numbers)
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should have a matrix of values' do
|
9
|
+
table([1,2]).should respond_to :matrix
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should generate the values matrix' do
|
13
|
+
table([1,2]).matrix.should be_kind_of(Array)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should generate a matrix with x + 1 rows' do
|
17
|
+
table([1,2]).matrix.length.should be_eql(3)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should generate a matrix with x + 1 columns' do
|
21
|
+
table([1,2]).matrix.first.length.should be_eql(3)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should have the first row as the header row' do
|
25
|
+
table([1,2]).matrix.first.should be_eql(['X', 1, 2])
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should have the first column as the header column' do
|
29
|
+
table([1,2]).matrix.map(&:first).should be_eql(['X', 1, 2])
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'a given coordinate of the matrix should be the product of its numbers' do
|
33
|
+
numbers = [5,6,4,7]
|
34
|
+
index_of_4 = numbers.index(4)
|
35
|
+
index_of_7 = numbers.index(7)
|
36
|
+
table(numbers).matrix[index_of_4 +1][index_of_7 +1].should be_eql(4 * 7)
|
37
|
+
table(numbers).matrix[index_of_7 +1][index_of_4 +1].should be_eql(4 * 7)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'a given coordinate of the matrix should be the product of its numbers' do
|
41
|
+
numbers = [1,2,3,4,5,6,7]
|
42
|
+
numbera = numbers.sample
|
43
|
+
numberb = numbers.sample
|
44
|
+
|
45
|
+
indexa = numbers.index(numbera)
|
46
|
+
indexb = numbers.index(numberb)
|
47
|
+
|
48
|
+
table(numbers).matrix[indexa + 1][indexb +1].should be_eql(numbera * numberb)
|
49
|
+
table(numbers).matrix[indexb + 1][indexa +1].should be_eql(numberb * numbera)
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should call the printer with the proper arguments' do
|
53
|
+
printer = PrimeTable::MultiplicationTable::PRINTER
|
54
|
+
matrix = table([1,2]).matrix
|
55
|
+
printer.should_receive(:new).with(:rows => matrix)
|
56
|
+
|
57
|
+
table([1,2]).print
|
58
|
+
end
|
59
|
+
end
|
data/spec/prime_spec.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'prime_table'
|
2
|
+
|
3
|
+
describe PrimeTable::Prime do
|
4
|
+
|
5
|
+
let(:primes) { [2,3,5,7,11,13,17,19,23,29] }
|
6
|
+
|
7
|
+
it 'should provide the integer instance with the is_prime? method' do
|
8
|
+
5.should respond_to(:is_prime?)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should return not prime for 1' do
|
12
|
+
1.is_prime?.should be_false
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should return not prime for 0' do
|
16
|
+
0.is_prime?.should be_false
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should return not prime for a negative number' do
|
20
|
+
-3.is_prime?.should be_false
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should return prime for a prime number' do
|
24
|
+
primes.sample.is_prime?.should be_true
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should provide the integer class with the n_first_primes method' do
|
28
|
+
Integer.should respond_to('n_first_primes')
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should return an array of numbers' do
|
32
|
+
Integer.n_first_primes.should be_kind_of(Array)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should return an array of 10 numbers by default' do
|
36
|
+
Integer.n_first_primes.length.should be_eql(10)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should return an array of X numbers' do
|
40
|
+
Integer.n_first_primes(23).length.should be_eql(23)
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should return all prime numbers' do
|
44
|
+
Integer.n_first_primes(23).all?(&:is_prime?).should be_true
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should return the first 10 primes numbers' do
|
48
|
+
Integer.n_first_primes.should be_eql(primes)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should provide the integer class with the (d+)_first_primes dynamic method' do
|
52
|
+
Integer.should respond_to("first_#{rand(100)}_primes")
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should return an array of X numbers' do
|
56
|
+
Integer.first_23_primes.length.should be_eql(23)
|
57
|
+
end
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prime_table
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Fernando Diaz
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-12 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Prints a multiplication table of prime numbers
|
15
|
+
email:
|
16
|
+
- fdiazgarrido@gmail.com
|
17
|
+
executables:
|
18
|
+
- prime_table
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- bin/prime_table
|
28
|
+
- features/prime_table.feature
|
29
|
+
- features/step_definitions/prime_table_definitions.rb
|
30
|
+
- features/support/setup.rb
|
31
|
+
- lib/prime_table.rb
|
32
|
+
- lib/prime_table/cli.rb
|
33
|
+
- lib/prime_table/multiplication_table.rb
|
34
|
+
- lib/prime_table/prime.rb
|
35
|
+
- lib/prime_table/version.rb
|
36
|
+
- prime_table.gemspec
|
37
|
+
- spec/multiplication_table_spec.rb
|
38
|
+
- spec/prime_spec.rb
|
39
|
+
homepage: ''
|
40
|
+
licenses: []
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.8.24
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Prints a multiplication table of prime numbers
|
63
|
+
test_files:
|
64
|
+
- features/prime_table.feature
|
65
|
+
- features/step_definitions/prime_table_definitions.rb
|
66
|
+
- features/support/setup.rb
|
67
|
+
- spec/multiplication_table_spec.rb
|
68
|
+
- spec/prime_spec.rb
|