prime_printer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +39 -0
- data/Rakefile +7 -0
- data/bin/prime_printer +12 -0
- data/lib/prime_printer/positional_printer.rb +11 -0
- data/lib/prime_printer/prime_generator.rb +29 -0
- data/lib/prime_printer/table_printer.rb +24 -0
- data/lib/prime_printer/utils.rb +12 -0
- data/lib/prime_printer/version.rb +3 -0
- data/lib/prime_printer.rb +20 -0
- data/prime_printer.gemspec +25 -0
- data/spec/prime_printer_spec.rb +31 -0
- data/spec/spec_helper.rb +79 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d01df13e8e54042eee180ddb56f868e2a6d82857
|
4
|
+
data.tar.gz: f2442e245aba46fcac1134ca06171567e2bf93fc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 670cee35cf84758ed06c2f56432501f3b4e37f92238aedb375a6c2aaab0a5c99a5ea4df0ca16c2d00ec1115b163a6a6f01714215b2f6549c870f0aebbd49e66f
|
7
|
+
data.tar.gz: bfdfb9b85c4623d7fd933677b6cd05cd0bf551aeaa152a9e071095b60995a5460b732edf8eba37182a2f1e70296c0057b25a013388da653d56f15c73221cc246
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Dimitar Bonev
|
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,39 @@
|
|
1
|
+
# PrimePrinter
|
2
|
+
|
3
|
+
Prime library for:
|
4
|
+
* printing prime multiplication table
|
5
|
+
* printing prime number at given position
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Note: requires ruby 2.1.0+
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'prime_printer'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install prime_printer
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
From command line run:
|
26
|
+
```prime_printer```
|
27
|
+
```prime_printer position 10```
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it ( https://github.com/[my-github-username]/prime_printer/fork )
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create a new Pull Request
|
36
|
+
|
37
|
+
## Tests
|
38
|
+
|
39
|
+
```bundle exec rake```
|
data/Rakefile
ADDED
data/bin/prime_printer
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'prime_printer'
|
3
|
+
|
4
|
+
if ARGV.size.even?
|
5
|
+
# convert CLI arguments to name-value argument pairs with name being symbol types
|
6
|
+
ARGV.map!.with_index { |arg, index| index.even? ? arg.to_sym : arg }
|
7
|
+
arg_pairs = Hash[*ARGV]
|
8
|
+
|
9
|
+
PrimePrinter.print_output **arg_pairs
|
10
|
+
else
|
11
|
+
puts "you need to provide even number of arguments or no arguments at all"
|
12
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class PrimePrinter::PositionalPrinter
|
2
|
+
def initialize(position:)
|
3
|
+
PrimePrinter::Utils.keyword_args_to_instance_vars_setter.call binding
|
4
|
+
end
|
5
|
+
|
6
|
+
def print_output
|
7
|
+
generator = PrimePrinter::PrimeGenerator.new
|
8
|
+
position.pred.times { generator.next }
|
9
|
+
puts generator.next
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class PrimePrinter::PrimeGenerator
|
2
|
+
def initialize
|
3
|
+
@generator = Fiber.new do
|
4
|
+
Fiber.yield 2
|
5
|
+
value = 3
|
6
|
+
loop do
|
7
|
+
Fiber.yield value if is_prime? value
|
8
|
+
value += 2
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def next(count = 1)
|
14
|
+
if count == 1
|
15
|
+
@generator.resume
|
16
|
+
else
|
17
|
+
[].tap do |primes|
|
18
|
+
count.times { primes.push @generator.resume }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def is_prime?(n)
|
26
|
+
Math.sqrt(n).floor.downto(2).each {|i| return false if n % i == 0}
|
27
|
+
true
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class PrimePrinter::TablePrinter
|
2
|
+
def initialize(primes: PrimePrinter::PrimeGenerator.new.next(10), cell_width: 4, col_delimiter: '|', row_delimiter: '—', empty_value: ' ' * cell_width)
|
3
|
+
PrimePrinter::Utils.keyword_args_to_instance_vars_setter.call binding
|
4
|
+
end
|
5
|
+
|
6
|
+
def print_output
|
7
|
+
header = print_row empty_value, primes.map(){ |p| int_to_cell(p) }.join
|
8
|
+
puts row_delimiter * header.length
|
9
|
+
|
10
|
+
primes.each do |p_row|
|
11
|
+
print_row int_to_cell(p_row), primes.map(){ |p_col| int_to_cell(p_row * p_col) }.join
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def print_row(header_col_value, body)
|
18
|
+
"#{header_col_value}#{col_delimiter}#{body}".tap { |row| puts row }
|
19
|
+
end
|
20
|
+
|
21
|
+
def int_to_cell(int)
|
22
|
+
"%#{cell_width}d" % int
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module PrimePrinter::Utils
|
2
|
+
def self.keyword_args_to_instance_vars_setter
|
3
|
+
proc do |bind|
|
4
|
+
bind.eval 'local_variables.each do |var|
|
5
|
+
instance_variable_set "@#{var}", binding.local_variable_get(var)
|
6
|
+
self.class.class_eval do
|
7
|
+
attr_reader var
|
8
|
+
end
|
9
|
+
end'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module PrimePrinter
|
2
|
+
require "prime_printer/positional_printer"
|
3
|
+
require "prime_printer/prime_generator"
|
4
|
+
require "prime_printer/table_printer"
|
5
|
+
require "prime_printer/utils"
|
6
|
+
require "prime_printer/version"
|
7
|
+
|
8
|
+
def self.print_output(**args)
|
9
|
+
if args.key? :position
|
10
|
+
PositionalPrinter.new(position: args[:position].to_i).print_output
|
11
|
+
elsif args.size > 0
|
12
|
+
puts ["Unexpected arguments #{args}",
|
13
|
+
"Usage examples: ",
|
14
|
+
"\t prime_printer",
|
15
|
+
"\t prime_printer position n # where n is a positive integer"].join("\n")
|
16
|
+
else
|
17
|
+
TablePrinter.new.print_output
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'prime_printer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "prime_printer"
|
8
|
+
spec.version = PrimePrinter::VERSION
|
9
|
+
spec.authors = ["Dimitar Bonev"]
|
10
|
+
spec.email = ["dsbonev@gmail.com"]
|
11
|
+
spec.summary = %q{Prints prime at given position and multiplication table of primes}
|
12
|
+
spec.description = %q{}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "simplecov"
|
25
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
describe PrimePrinter do
|
3
|
+
it 'should print out a multiplication table of the first 10 prime numbers' do
|
4
|
+
table = <<-TABLE
|
5
|
+
| 2 3 5 7 11 13 17 19 23 29
|
6
|
+
—————————————————————————————————————————————
|
7
|
+
2| 4 6 10 14 22 26 34 38 46 58
|
8
|
+
3| 6 9 15 21 33 39 51 57 69 87
|
9
|
+
5| 10 15 25 35 55 65 85 95 115 145
|
10
|
+
7| 14 21 35 49 77 91 119 133 161 203
|
11
|
+
11| 22 33 55 77 121 143 187 209 253 319
|
12
|
+
13| 26 39 65 91 143 169 221 247 299 377
|
13
|
+
17| 34 51 85 119 187 221 289 323 391 493
|
14
|
+
19| 38 57 95 133 209 247 323 361 437 551
|
15
|
+
23| 46 69 115 161 253 299 391 437 529 667
|
16
|
+
29| 58 87 145 203 319 377 493 551 667 841
|
17
|
+
TABLE
|
18
|
+
expect { subject.print_output }.to output(table).to_stdout
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should print out a prime number at given sequence position' do
|
22
|
+
position = 65_000
|
23
|
+
output = "#{814_279}\n"
|
24
|
+
expect { subject.print_output position: position }.to output(output).to_stdout
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'must run from the command line' do
|
28
|
+
output = `file bin/prime_printer`
|
29
|
+
output.should match(/ruby.+executable/)
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start
|
3
|
+
|
4
|
+
require_relative '../lib/prime_printer'
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
# rspec-expectations config goes here. You can use an alternate
|
8
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
9
|
+
# assertions if you prefer.
|
10
|
+
config.expect_with :rspec do |expectations|
|
11
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
12
|
+
# and `failure_message` of custom matchers include text for helper methods
|
13
|
+
# defined using `chain`, e.g.:
|
14
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
15
|
+
# # => "be bigger than 2 and smaller than 4"
|
16
|
+
# ...rather than:
|
17
|
+
# # => "be bigger than 2"
|
18
|
+
expectations.syntax = [:should, :expect]
|
19
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
20
|
+
end
|
21
|
+
|
22
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
23
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
24
|
+
config.mock_with :rspec do |mocks|
|
25
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
26
|
+
# a real object. This is generally recommended, and will default to
|
27
|
+
# `true` in RSpec 4.
|
28
|
+
mocks.verify_partial_doubles = true
|
29
|
+
end
|
30
|
+
|
31
|
+
# The settings below are suggested to provide a good initial experience
|
32
|
+
# with RSpec, but feel free to customize to your heart's content.
|
33
|
+
=begin
|
34
|
+
# These two settings work together to allow you to limit a spec run
|
35
|
+
# to individual examples or groups you care about by tagging them with
|
36
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
37
|
+
# get run.
|
38
|
+
config.filter_run :focus
|
39
|
+
config.run_all_when_everything_filtered = true
|
40
|
+
|
41
|
+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
42
|
+
# For more details, see:
|
43
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
44
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
45
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
46
|
+
config.disable_monkey_patching!
|
47
|
+
|
48
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
49
|
+
# be too noisy due to issues in dependencies.
|
50
|
+
config.warnings = true
|
51
|
+
|
52
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
53
|
+
# file, and it's useful to allow more verbose output when running an
|
54
|
+
# individual spec file.
|
55
|
+
if config.files_to_run.one?
|
56
|
+
# Use the documentation formatter for detailed output,
|
57
|
+
# unless a formatter has already been configured
|
58
|
+
# (e.g. via a command-line flag).
|
59
|
+
config.default_formatter = 'doc'
|
60
|
+
end
|
61
|
+
|
62
|
+
# Print the 10 slowest examples and example groups at the
|
63
|
+
# end of the spec run, to help surface which specs are running
|
64
|
+
# particularly slow.
|
65
|
+
config.profile_examples = 10
|
66
|
+
|
67
|
+
# Run specs in random order to surface order dependencies. If you find an
|
68
|
+
# order dependency and want to debug it, you can fix the order by providing
|
69
|
+
# the seed, which is printed after each run.
|
70
|
+
# --seed 1234
|
71
|
+
config.order = :random
|
72
|
+
|
73
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
74
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
75
|
+
# test failures related to randomization by passing the same `--seed` value
|
76
|
+
# as the one that triggered the failure.
|
77
|
+
Kernel.srand config.seed
|
78
|
+
=end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prime_printer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dimitar Bonev
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: ''
|
70
|
+
email:
|
71
|
+
- dsbonev@gmail.com
|
72
|
+
executables:
|
73
|
+
- prime_printer
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/prime_printer
|
84
|
+
- lib/prime_printer.rb
|
85
|
+
- lib/prime_printer/positional_printer.rb
|
86
|
+
- lib/prime_printer/prime_generator.rb
|
87
|
+
- lib/prime_printer/table_printer.rb
|
88
|
+
- lib/prime_printer/utils.rb
|
89
|
+
- lib/prime_printer/version.rb
|
90
|
+
- prime_printer.gemspec
|
91
|
+
- spec/prime_printer_spec.rb
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
homepage: ''
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.2.2
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Prints prime at given position and multiplication table of primes
|
117
|
+
test_files:
|
118
|
+
- spec/prime_printer_spec.rb
|
119
|
+
- spec/spec_helper.rb
|