prime-numbers 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ .DS_Store
2
+
3
+ *.gem
4
+ *.rbc
5
+ .bundle
6
+ .config
7
+ coverage
8
+ InstalledFiles
9
+ lib/bundler/man
10
+ pkg
11
+ rdoc
12
+ spec/reports
13
+ test/tmp
14
+ test/version_tmp
15
+ tmp
16
+
17
+ # YARD artifacts
18
+ .yardoc
19
+ _yardoc
20
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+
3
+ # Specify your gem's dependencies in prime-numbers.gemspec
4
+ gemspec
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ prime-numbers (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.3)
10
+ rspec (2.10.0)
11
+ rspec-core (~> 2.10.0)
12
+ rspec-expectations (~> 2.10.0)
13
+ rspec-mocks (~> 2.10.0)
14
+ rspec-core (2.10.1)
15
+ rspec-expectations (2.10.0)
16
+ diff-lcs (~> 1.1.3)
17
+ rspec-mocks (2.10.1)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ prime-numbers!
24
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Andrei Ursan
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.
@@ -0,0 +1,50 @@
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
20
+
21
+ require 'prime-numbers'
22
+
23
+ # initialize a generator object with a strategy, let's say the "one liner", which uses
24
+ # this algorithm to find the prime numbers:
25
+ # ('1' * number) !~ /^1?$|^(11+?)\1+$/
26
+ primes_generator = PrimeNumbers::Generator.new(:one_liner)
27
+
28
+ # or for a more performant strategy use
29
+ primes_generator = PrimeNumbers::Generator.new(:sieve_of_eratosthenes)
30
+
31
+ #then you can check for a specific prime number
32
+ prime_generator.is_prime? 947
33
+ => true
34
+
35
+ # or for prime numbers within a range
36
+ prime_generate.generate 3500, 3540
37
+ => [3511, 3517, 3527, 3529, 3533, 3539]
38
+
39
+ # inverse ranges are supported
40
+ prime_generate.generate 3540, 3500
41
+ => [3511, 3517, 3527, 3529, 3533, 3539]
42
+
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,32 @@
1
+ require "prime-numbers/version"
2
+ require "prime-numbers/algorithms/algorithm"
3
+ require "prime-numbers/errors/range_boundary_exception"
4
+
5
+ module PrimeNumbers
6
+ class Generator
7
+ def initialize algorithm_strategy
8
+ @algorithm = Algorithm.with_strategy(algorithm_strategy)
9
+ end
10
+
11
+ def generate a, b
12
+ a,b = range_sanitizer(a,b)[0], range_sanitizer(a,b)[1]
13
+ @algorithm.generate(a,b)
14
+ end
15
+
16
+ def is_prime? number
17
+ @algorithm.is_prime? number
18
+ end
19
+
20
+ private
21
+ def range_sanitizer a,b
22
+ if a <= 0 || a.class == Float
23
+ raise RangeBoundaryException, "#{a} is an invalid range boundary, please provide a positive integer instead!"
24
+ elsif b <= 0 || b.class == Float
25
+ raise RangeBoundaryException, "#{b} is an invalid range boundary, please provide a positive integer instead!"
26
+ end
27
+
28
+ a,b = b,a if a > b
29
+ [a,b]
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path('../one_liner', __FILE__)
2
+ require File.expand_path('../sieve_of_eratosthenes', __FILE__)
3
+
4
+ module PrimeNumbers
5
+ class Algorithm
6
+ ALGORITHMS = {
7
+ one_liner: AlgorithmOneLiner,
8
+ sieve_of_eratosthenes: AlgorithmSieveOfEratosthenes
9
+ }
10
+
11
+ def self.with_strategy strategy
12
+ ALGORITHMS[strategy].new
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module PrimeNumbers
2
+ class AlgorithmOneLiner
3
+ def generate a, b
4
+ primenumbers = []
5
+ (a..b).each do |number|
6
+ primenumbers << number if self.is_prime? number
7
+ end
8
+ primenumbers
9
+ end
10
+
11
+ def is_prime? number
12
+ number < 2 ? false : ('1' * number) !~ /^1?$|^(11+?)\1+$/
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ module PrimeNumbers
2
+ class AlgorithmSieveOfEratosthenes
3
+ def generate lower_bound, upper_bound
4
+ sqrt_upper = (Math.sqrt upper_bound).round
5
+ numbers = Array.new(upper_bound) {|e| e = false}
6
+ primes = []
7
+
8
+ (2 .. sqrt_upper).each do |m|
9
+ if !numbers[m]
10
+
11
+ if m >= lower_bound
12
+ primes << m
13
+ end
14
+
15
+ k = m * m
16
+ while k <= upper_bound
17
+ numbers[k] = true
18
+ k += m
19
+ end
20
+
21
+ end
22
+ end
23
+
24
+ (sqrt_upper+1 .. upper_bound).each do |n|
25
+ if !numbers[n] && n >= lower_bound
26
+ primes << n
27
+ end
28
+ end
29
+ primes
30
+ end
31
+
32
+ def is_prime? number
33
+ number < 2 ? false : !generate(number,number).empty?
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,2 @@
1
+ class RangeBoundaryException < Exception
2
+ end
@@ -0,0 +1,3 @@
1
+ module PrimeNumbers
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/prime-numbers/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Andrei Ursan"]
6
+ gem.email = ["hello@andreiursan.com"]
7
+ gem.description = %q{Prime Numbers Generator}
8
+ gem.summary = %q{Simply tells if a number is prime or generates al prime numbers from a provided range}
9
+ gem.homepage = "https://github.com/andreiursan/prime-numbers"
10
+
11
+ gem.add_development_dependency "rspec"
12
+
13
+ gem.files = `git ls-files`.split($\)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.name = "prime-numbers"
17
+ gem.require_paths = ["lib"]
18
+ gem.version = PrimeNumbers::VERSION
19
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe PrimeNumbers::AlgorithmOneLiner do
4
+
5
+
6
+ before(:all) do
7
+ @algorithm = PrimeNumbers::AlgorithmOneLiner.new
8
+ end
9
+
10
+ it "should return true for prime numbers" do
11
+ [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
12
+ 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,
13
+ 83, 89, 97, 101, 7901, 7907, 7919].each do |number|
14
+ @algorithm.is_prime?(number).should be_true
15
+ end
16
+ end
17
+
18
+ it "should return false for non-prime numbers" do
19
+ [832, 1, -23, 0, 63, 888812, 15].each do |number|
20
+ @algorithm.is_prime?(number).should be_false
21
+ end
22
+ end
23
+
24
+ it "should return prime numbers in an interval" do
25
+ @algorithm.generate(7900, 8000).should eq([7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 7963, 7993])
26
+ end
27
+
28
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe PrimeNumbers::AlgorithmSieveOfEratosthenes do
4
+
5
+
6
+ before(:all) do
7
+ @algorithm = PrimeNumbers::AlgorithmSieveOfEratosthenes.new
8
+ end
9
+
10
+ it "should return true for prime numbers" do
11
+ [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
12
+ 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,
13
+ 83, 89, 97, 101, 7901, 7907, 7919].each do |number|
14
+ @algorithm.is_prime?(number).should be_true
15
+ end
16
+ end
17
+
18
+ it "should return false for non-prime numbers" do
19
+ [832, 1, -23, 0, 63, 888812, 15].each do |number|
20
+ @algorithm.is_prime?(number).should be_false
21
+ end
22
+ end
23
+
24
+ it "should retunr prime numbers in an interval" do
25
+ @algorithm.generate(7900, 8000).should eq([7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 7963, 7993])
26
+ end
27
+
28
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe RangeBoundaryException do
4
+
5
+ it "should raise a RangeBoundaryException" do
6
+ begin
7
+ raise RangeBoundaryException
8
+ rescue RangeBoundaryException => error
9
+ error.class.should == RangeBoundaryException
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe PrimeNumbers::PrimeNumberGenerator do
4
+
5
+ before(:all) do
6
+ @prime_generator = PrimeNumbers::Generator.new(:one_liner)
7
+ end
8
+
9
+ it "should respond to message generate" do
10
+ @prime_generator.should respond_to(:generate)
11
+ end
12
+
13
+ it "should respond to message is_prime?" do
14
+ @prime_generator.should respond_to(:is_prime?)
15
+ end
16
+
17
+ it "should return all prime numbers between 1 and 13" do
18
+ @prime_generator.generate(1, 13).should eq([2, 3, 5, 7, 11, 13])
19
+ end
20
+
21
+ it "should handle reverse ranges (20,1) == (1,20)" do
22
+ begin
23
+ @prime_generator.generate(20, 1).should eq([2, 3, 5, 7, 11, 13, 17, 19])
24
+ raise RangeBoundaryException
25
+ rescue RangeBoundaryException => error
26
+ error.class.should == RangeBoundaryException
27
+ end
28
+ end
29
+
30
+ it "should raise an error on negtive range boundaries" do
31
+ begin
32
+ @prime_generator.generate(20, -2)
33
+ rescue RangeBoundaryException => error
34
+ error.should_not eq(nil)
35
+ error.message.should eq("-2 is an invalid range boundary, please provide a positive integer instead!")
36
+ end
37
+ end
38
+
39
+ it "should raise an error on float numbers range boundaries" do
40
+ begin
41
+ @prime_generator.generate(20.5, 2)
42
+ rescue RangeBoundaryException => error
43
+ error.should_not eq(nil)
44
+ error.message.should eq("20.5 is an invalid range boundary, please provide a positive integer instead!")
45
+ end
46
+ end
47
+
48
+ it "should return true for a prime number (17)" do
49
+ @prime_generator.is_prime?(17).should be_true
50
+ end
51
+
52
+ it "should return false if a number isn't prime (18)" do
53
+ @prime_generator.is_prime?(18).should be_false
54
+ end
55
+
56
+ end
@@ -0,0 +1,4 @@
1
+ require_relative "../lib/prime-numbers.rb"
2
+ require_relative "../lib/prime-numbers/algorithms/one_liner.rb"
3
+ require_relative "../lib/prime-numbers/algorithms/sieve_of_eratosthenes.rb"
4
+ require_relative "../lib/prime-numbers/errors/range_boundary_exception.rb"
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prime-numbers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrei Ursan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
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
+ description: Prime Numbers Generator
31
+ email:
32
+ - hello@andreiursan.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - lib/prime-numbers.rb
44
+ - lib/prime-numbers/algorithms/algorithm.rb
45
+ - lib/prime-numbers/algorithms/one_liner.rb
46
+ - lib/prime-numbers/algorithms/sieve_of_eratosthenes.rb
47
+ - lib/prime-numbers/errors/range_boundary_exception.rb
48
+ - lib/prime-numbers/version.rb
49
+ - prime-numbers.gemspec
50
+ - spec/prime-numbers/algorithms/one_liner_spec.rb
51
+ - spec/prime-numbers/algorithms/sieve_of_eratosthenes_spec.rb
52
+ - spec/prime-numbers/errors/range_boundary_exception_spec.rb
53
+ - spec/prime-numbers_spec.rb
54
+ - spec/spec_helper.rb
55
+ homepage: https://github.com/andreiursan/prime-numbers
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.24
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Simply tells if a number is prime or generates al prime numbers from a provided
79
+ range
80
+ test_files:
81
+ - spec/prime-numbers/algorithms/one_liner_spec.rb
82
+ - spec/prime-numbers/algorithms/sieve_of_eratosthenes_spec.rb
83
+ - spec/prime-numbers/errors/range_boundary_exception_spec.rb
84
+ - spec/prime-numbers_spec.rb
85
+ - spec/spec_helper.rb