prime_numbers 0.0.3

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 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_numbers.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Adit
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,63 @@
1
+ # PrimeNumbers
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'prime_numbers'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install prime_numbers
18
+
19
+ ## Usage (in terminal)
20
+
21
+ > PrimeNumbers.table_product_primes(10)
22
+
23
+
24
+ ## WHAT I'VE DONE
25
+
26
+
27
+ 1. I've looked around for some existing code ;)
28
+
29
+
30
+ - never reinvent from scratch
31
+
32
+ - 99% of the time somebody has already done something I need to accomplish.
33
+ 70% of the time it's the EXACT same request (first 10 prime numbers!)
34
+
35
+ - I want to learn to "copy the good stuff" and "enhance"
36
+
37
+ - chances are you get stuff done first and well tested too
38
+
39
+
40
+ 2. Once I found some good piece of code I spend my precious time to:
41
+
42
+ a) test it's copy-pasted code behaviour
43
+ b) encapsulate into organized structure
44
+ c) package the whole stuff and ship for good
45
+
46
+ enjoy!
47
+
48
+
49
+
50
+ ## SOME NOTES
51
+
52
+ Just to add something of my own I've added some code extending functionality:
53
+
54
+ - monkey patching is considered a bad practice if done wildly
55
+ - better do it "with style", I've tried to implement new beta function "refine"
56
+
57
+
58
+
59
+ ## REFS
60
+
61
+ - first N prime numbers: z5h from http://stackoverflow.com/questions/11673968/how-do-i-generate-the-first-n-prime-numbers
62
+ - how to monkey patch (BETA): http://www.sitepoint.com/a-look-at-ruby-2-0/
63
+ - build a gem: http://railscasts.com/episodes/245-new-gem-with-bundler?view=asciicast
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = 'spec/**/*_spec.rb'
7
+ spec.rspec_opts = ['--color']
8
+ end
9
+ task :default => :spec
10
+ rescue LoadError
11
+ nil
12
+ end
@@ -0,0 +1,20 @@
1
+ module PrimeInt
2
+
3
+ class Integer
4
+
5
+ def self.is_prime?(num)
6
+ return false if num <= 1
7
+ 2.upto(Math.sqrt(num).to_i) do |x|
8
+ return false if num % x == 0
9
+ end
10
+ true
11
+ end
12
+
13
+ def self.next_prime(num)
14
+ n = num + 1
15
+ n = n + 1 until is_prime?(n)
16
+ n
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,3 @@
1
+ module PrimeNumbers
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,41 @@
1
+ require "terminal-table"
2
+ require "prime_numbers/version"
3
+ require "prime_numbers/prime_int"
4
+
5
+ module PrimeNumbers
6
+
7
+ def self.fist_primes(n = 10)
8
+ e = Enumerator.new do |els|
9
+ i = 2
10
+ loop do
11
+ els << i
12
+ i = PrimeInt::Integer.next_prime(i)
13
+ end
14
+ end
15
+ e.take n
16
+ end
17
+
18
+ def self.first_prime_prods(n = 10)
19
+ return 0 unless n > 1
20
+
21
+ rows = []
22
+ next_prime = 0
23
+ prime_prod = 1
24
+
25
+ (n - 1).times do |i|
26
+
27
+ next_prime = PrimeInt::Integer.next_prime(next_prime)
28
+ prime_prod = prime_prod * next_prime
29
+
30
+ rows << [i, next_prime, prime_prod]
31
+ end
32
+
33
+ rows
34
+ end
35
+
36
+ def self.table_product_primes(n = 10)
37
+ # This is just a binding from an external plugin which can be modified / substituted during time
38
+ Terminal::Table.new :headings => ['i', 'Prime', 'Product'], :rows => first_prime_prods(n)
39
+ end
40
+
41
+ end
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'prime_numbers/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "prime_numbers"
8
+ gem.version = PrimeNumbers::VERSION
9
+ gem.authors = ["Adit"]
10
+ gem.email = ["a.saxena@email.it"]
11
+ gem.description = "Write a program that calculates and prints out a multiplication table of the first 10 calculated prime numbers."
12
+ gem.summary = "The program must run from the command line and print to screen 1 table.\nAcross the top and down the left side should be the 10 primes, and the body of the table should contain the product of multiplying these numbers."
13
+
14
+ gem.homepage = ""
15
+
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
+
19
+ gem.files = `git ls-files`.split($/)
20
+ # s.files = Dir["[A-Z]*[^~]"] + Dir["lib/**/*.rb"] + Dir["spec/*"] + [".gitignore"]
21
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
22
+
23
+ gem.require_paths = ["lib"]
24
+
25
+ gem.add_dependency "terminal-table"
26
+
27
+ gem.add_development_dependency "rake"
28
+ gem.add_development_dependency "rspec"
29
+ gem.add_development_dependency "awesome_print"
30
+
31
+ end
@@ -0,0 +1,74 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ # WARNING: this works for short numbers! (takes too much memory)
4
+ class PrimeTester
5
+ def self.is_prime?(n)
6
+ ("1" * n) !~ /^1?$|^(11+?)\1+$/
7
+ end
8
+ end
9
+
10
+ describe PrimeNumbers do
11
+
12
+ # A good check implements different calculation methods: regex magic != manual loop
13
+ it "test 100 sample numbers to check if our prime calculator return valid number" do
14
+
15
+ 100.times do |i|
16
+
17
+ is_prime_regx = PrimeTester.is_prime? i
18
+ is_prime_our_class = PrimeInt::Integer.is_prime?(i)
19
+
20
+ is_prime_regx.should eq(is_prime_our_class)
21
+
22
+ end
23
+
24
+ end
25
+
26
+ it "Raise an error for invalid product" do
27
+ PrimeNumbers.first_prime_prods(0).should eq 0
28
+ PrimeNumbers.first_prime_prods(-1).should eq 0
29
+ PrimeNumbers.first_prime_prods(-100).should eq 0
30
+ end
31
+
32
+ it "Test a multiplication table of the first 10 calculated prime numbers" do
33
+ qty = 10
34
+
35
+ qty.should be > 1
36
+
37
+ first_primes = PrimeNumbers.fist_primes(qty)
38
+ first_primes_prods = PrimeNumbers.first_prime_prods(qty)
39
+
40
+ manual_prod = 1
41
+
42
+ (qty - 1).times do |i|
43
+
44
+ manual_prod = manual_prod * first_primes[i]
45
+ manual_prod.should eq first_primes_prods[i][2]
46
+
47
+ end
48
+ end
49
+
50
+ it "Prints out a multiplication table of the first 10 calculated prime numbers" do
51
+ puts PrimeNumbers.table_product_primes(10)
52
+ end
53
+
54
+ it "Benchmarks our performance" do
55
+ require 'prime'
56
+
57
+ Benchmark.bm(7) do |bm|
58
+ bm.report("Ruby Core Prime:") { Prime.prime?(20_000) }
59
+ bm.report("Our prime validator:") { PrimeInt::Integer.is_prime?(20_000) }
60
+ end
61
+
62
+ end
63
+
64
+ it "this Benchmark compares our prime lister vs prime products" do
65
+ require 'prime'
66
+
67
+ Benchmark.bm(7) do |bm|
68
+ bm.report("list primes only:") { PrimeNumbers.fist_primes(20_000) }
69
+ bm.report("list primes and its product:") { PrimeNumbers.first_prime_prods(20_000) }
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require "benchmark"
5
+ require "awesome_print"
6
+ require "prime_numbers"
7
+
8
+
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prime_numbers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adit
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: terminal-table
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: awesome_print
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Write a program that calculates and prints out a multiplication table
79
+ of the first 10 calculated prime numbers.
80
+ email:
81
+ - a.saxena@email.it
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - !binary |-
87
+ LmdpdGlnbm9yZQ==
88
+ - !binary |-
89
+ R2VtZmlsZQ==
90
+ - !binary |-
91
+ TElDRU5TRS50eHQ=
92
+ - !binary |-
93
+ UkVBRE1FLm1k
94
+ - !binary |-
95
+ UmFrZWZpbGU=
96
+ - !binary |-
97
+ bGliL3ByaW1lX251bWJlcnMucmI=
98
+ - !binary |-
99
+ bGliL3ByaW1lX251bWJlcnMvcHJpbWVfaW50LnJi
100
+ - !binary |-
101
+ bGliL3ByaW1lX251bWJlcnMvdmVyc2lvbi5yYg==
102
+ - !binary |-
103
+ cHJpbWVfbnVtYmVycy5nZW1zcGVj
104
+ - !binary |-
105
+ c3BlYy9wcmltZV9udW1iZXJzX3NwZWMucmI=
106
+ - !binary |-
107
+ c3BlYy9zcGVjX2hlbHBlci5yYg==
108
+ homepage: ''
109
+ licenses: []
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 1.8.24
129
+ signing_key:
130
+ specification_version: 3
131
+ summary: The program must run from the command line and print to screen 1 table. Across
132
+ the top and down the left side should be the 10 primes, and the body of the table
133
+ should contain the product of multiplying these numbers.
134
+ test_files:
135
+ - !binary |-
136
+ c3BlYy9wcmltZV9udW1iZXJzX3NwZWMucmI=
137
+ - !binary |-
138
+ c3BlYy9zcGVjX2hlbHBlci5yYg==
139
+ has_rdoc: