prime_table_funding 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1cb6f86e5127afb1dd6ab7138de1053f75a49718
4
+ data.tar.gz: 5136d0511555fa295bb68bb6c9350b4e60e71e71
5
+ SHA512:
6
+ metadata.gz: aea77aa77b585ca555afa7cb08b27180570ea4b46ba6787c37f959323a68e3a46026eccabb77049d714db17a1c9dc0b3d714a871450f6e5a153cbac343899819
7
+ data.tar.gz: 3a7df33757bf885fda12c12bc997371fa01da9c0f339d0c26294f296174b993d7307af4b5a040e56cc982c6a2fcca5bc4460ed0cd51fdfe1b19c96215f17d1af
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in prime_table.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Lee Kiernan
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,22 @@
1
+ $ gem install prime_table
2
+
3
+
4
+ # Notes
5
+ def sieve(upper)
6
+ i = 0
7
+ list = (2..upper).to_a
8
+
9
+ (2..Math.sqrt(upper)).each do |mult|
10
+
11
+ if list[i] #proceed only when mult is prime
12
+ init = mult + i
13
+
14
+ (init..upper-1).step(mult) do |index|
15
+ list[index] = nil
16
+ end
17
+
18
+ end
19
+ i += 1
20
+ end
21
+ list.compact
22
+ end
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+ task :default => :spec
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ require "prime_table/version"
3
+ require_relative "prime_table/prime_table"
4
+
5
+ gh = PrimeTable::Optimus.new
6
+ gh.get_input
@@ -0,0 +1,96 @@
1
+ module Calculate
2
+ def self.is_prime?(n)
3
+ n = n.to_i
4
+ return false if n < 2
5
+
6
+ # http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/
7
+ # ("1" * n) !~ /^1?$|^(11+?)\1+$/
8
+
9
+ # [*2...n].each { |block_n| return false unless n % block_n }
10
+
11
+ # Suppose one number is a factor of N and that it is smaller than the square-root of the number N.
12
+ # Then the second factor must be larger than the square-root.
13
+ [*2..Math.sqrt(n)].reject { |block_n| n % block_n > 0 }.empty?
14
+ end
15
+
16
+ def self.next_prime(from)
17
+ # Don't start from 1, because silly.
18
+ from += 1 until self.is_prime?(from)
19
+ from
20
+ end
21
+
22
+ def self.n_primes(n = 10)
23
+ last_prime = 0
24
+ primes = []
25
+
26
+ while n > 0 do
27
+ last_prime = self.next_prime(last_prime + 1)
28
+ primes << last_prime
29
+ n -= 1
30
+ end
31
+
32
+ primes
33
+ end
34
+ end
35
+
36
+ module Display
37
+ def self.for_primes(count, size)
38
+ "%#{size}d |" * count
39
+ end
40
+
41
+ def self.table(primes)
42
+ # Get largest number - to ensure the table cells are even and fit.
43
+ last_element = primes.sort.reverse.first
44
+ element_size = (last_element*last_element).to_s.length
45
+ element_size = element_size < 4 ? 4 : element_size + 1
46
+
47
+ $stdout.printf "%#{element_size+2}s #{ self.for_primes(primes.count, element_size) }\n", '', *primes
48
+ $stdout.printf "%#{element_size+2}s #{ "%#{element_size}s |" * primes.count }\n", '', *primes.map { |s| '-' * element_size }
49
+
50
+ primes.each do |prime|
51
+ $stdout.printf "%#{element_size+1}d -#{self.for_primes(primes.count, element_size) }\n", prime, *primes.map { |n| n*prime }
52
+ end
53
+ end
54
+ end
55
+
56
+ module PrimeTable
57
+ class Optimus
58
+ attr_accessor :input, :output, :primes
59
+
60
+ include Calculate
61
+ include Display
62
+
63
+ # Get and parse user input.
64
+
65
+ def initialize
66
+ @primes = []
67
+ end
68
+
69
+ def get_input
70
+ $stdout.puts "'q' to escape"
71
+ $stdout.print "Enter the required number of primes, or press enter to display the first #{@primes.count > 0 ? @primes.count : 10}: "
72
+ parse_input $stdin.gets.chomp
73
+ end
74
+
75
+ def parse_input(input)
76
+ return if input == "q"
77
+
78
+ if input.empty?
79
+ input = @primes.empty? ? 10 : @primes.count
80
+ end
81
+
82
+ input = input.to_i
83
+ if input < 2
84
+ $stdout.puts "Orly? Here you go! []"
85
+ return get_input
86
+ end
87
+
88
+ @primes = Calculate::n_primes(input)
89
+ Display::table(@primes)
90
+
91
+ return get_input
92
+ end
93
+
94
+ end
95
+ end
96
+
@@ -0,0 +1 @@
1
+ require "prime_table/version"
@@ -0,0 +1,3 @@
1
+ module PrimeTable
2
+ VERSION = "0.0.1"
3
+ 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_table/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "prime_table_funding"
8
+ spec.version = PrimeTable::VERSION
9
+ spec.authors = ["Lee Kiernan"]
10
+ spec.email = ["lee.kiernan@gmail.com"]
11
+ spec.description = %q{Generate and display prime number table in the terminal}
12
+ spec.summary = %q{Generate and display prime number table in the terminal}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3"
22
+ spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "bahia"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe PrimeTable, 'prompt' do
5
+ before do
6
+ @optimus = PrimeTable::Optimus.new
7
+ IO.any_instance.stub(:puts)
8
+ end
9
+
10
+ it "should return if quitted" do
11
+ @optimus.parse_input('q').should be_nil
12
+ end
13
+
14
+ it "should act as 10 if nothing given" do
15
+ end
16
+
17
+ describe "when given stuff" do
18
+ # before { @optimus.parse_input('0') }
19
+
20
+ it "should give generic response to 0/1/non-number" do
21
+ # SpecHelper.local_io.should == "Orly? Here you go! []"
22
+ end
23
+ end
24
+
25
+ context Calculate do
26
+ it "should get primes correctly" do
27
+ Calculate.n_primes(10).should have(10).items
28
+ end
29
+
30
+ it "should get next prime given number" do
31
+ Calculate.next_prime(168).should equal(173)
32
+ end
33
+
34
+ it "should report primes correctly" do
35
+ Calculate.is_prime?(3).should be_true
36
+ Calculate.is_prime?(30).should be_false
37
+ Calculate.is_prime?(173).should be_true
38
+ end
39
+ end
40
+
41
+ context Display do
42
+ it "should display table well" do
43
+ # primes = [2,3,5,7]
44
+ # Display.table(primes).should_receive(:stdout).with """
45
+ # 2 | 3 | 5 | 7 |
46
+ # ---- |---- |---- |---- |
47
+ # 2 - 4 | 6 | 10 | 14 |
48
+ # 3 - 6 | 9 | 15 | 21 |
49
+ # 5 - 10 | 15 | 25 | 35 |
50
+ # 7 - 14 | 21 | 35 | 49 |
51
+ # """
52
+ end
53
+
54
+ it "should give formatted strings for display" do
55
+ expect(Display.for_primes(1, 5)).to eq "%5d |"
56
+ expect(Display.for_primes(2, 5)).to eq "%5d |%5d |"
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,25 @@
1
+ # require 'rubygems'
2
+ # require 'bundler/setup'
3
+ require 'bahia'
4
+ require_relative '../bin/prime_table/prime_table.rb'
5
+
6
+ # Bahia.project_directory = '..'
7
+ RSpec.configure do |config|
8
+ config.include Bahia
9
+ # config.before(:all, &:silence_output)
10
+ # config.after(:all, &:enable_output)
11
+ end
12
+
13
+ module SpecHelper
14
+ def local_io(in_str)
15
+ old_stdin, old_stdout = $stdin, $stdout
16
+
17
+ $stdin = StringIO.new(in_str)
18
+ $stdout = StringIO.new
19
+ yield
20
+
21
+ $stdout.string
22
+ ensure
23
+ $stdin, $stdout = old_stdin, old_stdout
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prime_table_funding
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Lee Kiernan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-19 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
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: bahia
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: rake
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: Generate and display prime number table in the terminal
70
+ email:
71
+ - lee.kiernan@gmail.com
72
+ executables:
73
+ - prime_table.rb
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .DS_Store
78
+ - .gitignore
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/prime_table.rb
84
+ - bin/prime_table/prime_table.rb
85
+ - lib/prime_table.rb
86
+ - lib/prime_table/version.rb
87
+ - prime_table.gemspec
88
+ - spec/prime_table_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: ''
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.1.10
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Generate and display prime number table in the terminal
114
+ test_files:
115
+ - spec/prime_table_spec.rb
116
+ - spec/spec_helper.rb